Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 通过电子邮件从缓存目录发送文件_Android - Fatal编程技术网

Android 通过电子邮件从缓存目录发送文件

Android 通过电子邮件从缓存目录发送文件,android,Android,我正在尝试从我的应用程序发送文件附加电子邮件 保存到外部存储器(SD卡)中的文件可以成功附加,但无法附加保存到临时目录中的同一文件,我可以在该临时目录中获取getCacheDir()method 唯一的区别是我要附加的文件保存在哪里, 这是因为Android规范或限制,还是我遗漏了什么 我正在使用ACTION\u SEND意图通过电子邮件发送附件文件 //// getting file path to save //[fail] -- no attatchment in email //path

我正在尝试从我的应用程序发送文件附加电子邮件

保存到外部存储器(SD卡)中的文件可以成功附加,但无法附加保存到临时目录中的同一文件,我可以在该临时目录中获取
getCacheDir()
method

唯一的区别是我要附加的文件保存在哪里, 这是因为Android规范或限制,还是我遗漏了什么

我正在使用
ACTION\u SEND
意图通过电子邮件发送附件文件

//// getting file path to save
//[fail] -- no attatchment in email
//path = new StorageUtil().getCacheFilePath(this, "attatchment.html");
//[success] -- attatchment.html is on email
path = new StorageUtil().getExternalAppStoragePath(this, "attatchment.html");
/// start intent if file saving is successful
if (this.export(path)==true) {
    Intent i = new Intent();
    i.setAction(Intent.ACTION_SEND);
    i.setType("text/html"); 
    i.putExtra(Intent.EXTRA_SUBJECT, "a subject");
    i.putExtra(Intent.EXTRA_TEXT, "");
    File f = new File(path);
    i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
    startActivity(Intent.createChooser(i, "Send Email"));
}
getCacheFilePath()通过以下代码创建路径,其中fileName是该方法的第二个参数:

File cacheDir = ctx.getCacheDir();
File cacheFile = new File(cacheDir, fileName);
return cacheFile.getPath(); //path
每个路径如下所示

//cache dir (attachcment failed): 
/data/data/{PACKAGE_NAME}/cache/attachment.html

//external dir (attachment successed): 
/mnt/sdcard/Android/data/{PACKAGE_NAME}/files/attachment.html
从缓存目录
canRead()
获取文件对象,并可以获取文件长度。 谢谢

==已解决==

我在发送Gmail时发现Logcat上有以下错误:

file:// attachment paths must point to file:///mnt/sdcard. Ignoring attachment file:///data/data/{PACKAGE_NAME}/cache/attachment.html"

所以这应该是Android的一个限制。添加
Intent.FLAG\u GRANT\u READ\u URI\u PERMISSION
没有效果,Dropbox等其他活动的结果也类似。

我建议您使用getExternalCacheDir()而不是getCacheDir(),后者指向“/storage/emulated/0/Android/data/”。此文件夹下的文件似乎可以附加到gmail。

我建议您使用getExternalCacheDir()而不是getCacheDir(),后者指向“/storage/emulated/0/Android/data/”。此文件夹下的文件似乎可以附加到gmail。

你能粘贴你的代码吗?你的
路径包含什么?谢谢我添加了getCacheFilePath代码!还从日志上传了每个路径!谢谢你能粘贴你的代码吗?你的
路径包含什么?谢谢我添加了getCacheFilePath代码!还从日志上传了每个路径!谢谢你