2

I am trying to send file attached email from my App.

The file saved into external storage(SDCard) can successfully attached, but the same file that is saved into temporary directory where I can get getCacheDir() method cannot be attached.

The only difference is to where the file I want to attatch is saved, Is this because of an Android spec or limitation, or am I missing something?

I'm using ACTION_SEND intent to send attachment file via email

//// 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() creates path from following code where fileName is the second argument of the method:

File cacheDir = ctx.getCacheDir();
File cacheFile = new File(cacheDir, fileName);
return cacheFile.getPath(); //path

each path is as follows

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

//external dir (attachment successed): 
/mnt/sdcard/Android/data/{PACKAGE_NAME}/files/attachment.html

File object from the cache dir canRead() and could obtain file length. thanks!

== SOLVED ==

I found that the following error is on Logcat when sending Gmail:

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

So this should be a Android limitation. Adding Intent.FLAG_GRANT_READ_URI_PERMISSION has no effect and other Activity like Dropbox results similar.

ppreetikaa
  • 1,149
  • 2
  • 15
  • 22
Kurosawa Hiroyuki
  • 1,217
  • 2
  • 14
  • 22

1 Answers1

0

I suggest you to use getExternalCacheDir() instead of getCacheDir(), which point to "/storage/emulated/0/Android/data/". File under this folder seems can be attached to gmail.

Hexise
  • 1,520
  • 15
  • 20