3

Im working on android and trying to send an email with a file attached but getting the following error.

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SENDTO dat=file://assets/Cards/myfile.pdf typ=Applications/pdf flg=0x10000000 (has extras) }

Im very new to android dev so Im kinda lost as to what i need to do . Below is what I am currently trying

Intent intent = new Intent(Intent.ACTION_SENDTO ); // it's not ACTION_SEND
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Card Set "));
intent.putExtra(Intent.EXTRA_TEXT, "");
intent.setData(Uri.parse("mailto:"));
intent.setDataAndType(Uri.parse("file://assets/Cards/" + "myfile.pdf"),
                                                          "Applications/pdf");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
activity.startActivity(intent);

I can create and send emails fine without attaching a file. Any help on how i should be attaching file correctly would be great thanks

glogic
  • 4,042
  • 3
  • 28
  • 44
  • Test it in real device NOT IN EMULATOR. If that doesnt help then try setting up intent.setType("application/pdf"); and see the result. – Kartik Domadiya Sep 13 '12 at 12:45
  • yeah ive been testing on device only and have tried setting application/pdf but it gives me the same error – glogic Sep 13 '12 at 12:50
  • Try setting up your intent to Intent.ACTION_SEND. Correct MIME type for pdf files is "application/pdf" not "Applications/pdf" – Kartik Domadiya Sep 13 '12 at 12:51
  • cheers for correcting me on the mime type. silly mistake on my part. now it is putting the path to the file "file://assets/cards/myfile.pdf" in the receiver section of email. but still no attachment – glogic Sep 13 '12 at 13:03
  • Change the path to "file:///android_asset/cards/myfile.pdf". Make sure your folder name is "Cards" or "cards". Use that accordingly. – Kartik Domadiya Sep 13 '12 at 13:06
  • afraid no joy. i had tried that already when seen it on a different post. – glogic Sep 13 '12 at 13:11
  • 1
    Refer http://stackoverflow.com/questions/8093907/how-to-get-the-pdf-file-from-assets-in-android?rq=1 – Kartik Domadiya Sep 13 '12 at 13:19
  • cheers for that link. put me on the right path. im not storing the files in external memory and attaching it from there. Im going give full answer below – glogic Sep 13 '12 at 15:14

2 Answers2

1

Ok thanks to Kartik's link i managed to track down the correct way (well a working way) to attach a file. After searching around i found this to show how to store files to external memory http://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory%28java.lang.String%29

so i use a modified createExternalStoragePublicPicture() that is on this page to write to memory and then i do the following

createExternalStoragePublicPicture();
File path = Environment.getExternalStoragePublicDirectory(
                                                Environment.DIRECTORY_PICTURES);
                                        File file = new File(path, "cards_01.pdf");
Intent intent = new Intent(Intent.ACTION_SEND ,Uri.parse("mailto:")); // it's not ACTION_SEND
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Card Set ");
intent.putExtra(Intent.EXTRA_TEXT, "");
intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this will make such that when user returns to your app, your app is displayed, instead of the email app.
activity.startActivity(intent);

The link above shows how to delete the files also and says about putting them in correct folders to avoid overwriting other files. Hope this helps any others with same issues

glogic
  • 4,042
  • 3
  • 28
  • 44
0

I think this example mi8 help your scenario:)

Manoj Kumar
  • 1,510
  • 3
  • 20
  • 40