2

My app generates kml (xml) files that I would like the user to be able to share via email, bluetooth, or whatever. I've looked at dozens of examples but am obviously still doing something wrong. If I select Gmail, the email loads properly, but the file isn't attached. If I select bluetooth, the bluetooth manager crashes. What have I done wrong here?

private void createShareIntent(String filename) {
    Log.d(Common.APP_TAG, "** Create share intent **");
  String mime = "text/plain";
    File exportFile = new File(filename);
    Log.d(Common.APP_TAG, Uri.fromFile(exportFile).toString());
    // logcat says : file:///mnt/sdcard/Android/data/com.gmail.qkzoo1978.qwhereami/files/exports/qExport_11-6-2012_1648.kml
    Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"testing@gmail.com"});
    intent.putExtra(Intent.EXTRA_SUBJECT, "Test");
    intent.putExtra(Intent.EXTRA_TEXT, "Text");
    intent.setType(mime);
    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(exportFile));
    //intent.setDataAndType(Uri.fromFile(exportFile),  mime);
    startActivity(Intent.createChooser(intent, "Share Kml"));
}  
dymmeh
  • 22,247
  • 5
  • 53
  • 60
qkzoo1978
  • 315
  • 3
  • 11
  • Have you tried selecting a file outside of the data folder? Android might not give you permission to read from there – dymmeh Nov 06 '12 at 23:07
  • http://stackoverflow.com/questions/587917/trying-to-attach-a-file-from-sd-card-to-email [This thread][1] has alot of useful information. [1]: http://stackoverflow.com/questions/587917/trying-to-attach-a-file-from-sd-card-to-email – Jason Hessley Nov 06 '12 at 23:44
  • I haven't tried with the same file anywhere else, but it let's me read/write here, this app created this file. The directory above is my apps directory on the sdcard. – qkzoo1978 Nov 07 '12 at 00:26
  • I checked the thread you linked too, I actually read the same one earlier. The difference is, the file isn't being attached at all in my case. – qkzoo1978 Nov 07 '12 at 00:30
  • An individual on irc android-dev (maha) pointed out that I forgot the Intent.ACTION_SEND: Intent intent = new Intent(Intent.ACTION_SEND); – qkzoo1978 Nov 07 '12 at 15:07

1 Answers1

0

Not sure if you ever solved this but I ran into the same problem this weekend and realized it was because I was saving a file that my app has access to but other apps do not. So when the application used to handle the share tries to read the file permission is denied. Oddly this isn't really clear in the log.

The way to solve this is either to save the file to external storage, which might not be ideal.

Or as a better solution use a File provider and grant permission in the intent.

Ben
  • 1,285
  • 1
  • 9
  • 15