0

I want to attach a .txt file in email for sending in Android. The file is located at /data/data/com.PackageName/files/. I am trying as;

 private void Share(String file_name) {
        try {
            String filelocation = "/data/data/com.PackageName/files/awais.txt";
            Intent intent = new Intent(Intent.ACTION_SENDTO);
            intent.setType("text/plain");
            String message="File to be shared is " + file_name + ".";
            intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
            intent.putExtra(Intent.EXTRA_STREAM, Uri.parse( "file://"+filelocation));
            intent.putExtra(Intent.EXTRA_TEXT, message);
            intent.setData(Uri.parse("mailto:xyz@gmail.com"));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            startActivity(intent);
        } catch(Exception e)  {
            System.out.println("is exception raises during sending mail"+e);
        }

}

I have added the permissions in the AndroidMenifest as

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>  

But Android gives the error.

Permission denied for the attachment.

For the more clarification see the picture below.

enter image description here

Any help?

Community
  • 1
  • 1
Humty
  • 1,321
  • 3
  • 18
  • 35
  • Take a look at this post: http://stackoverflow.com/questions/30016888/send-text-file-attachment-with-an-email – hasanzuav Mar 28 '17 at 06:30
  • I have never played with this, but I'm quite sure you can't convert an URI to a stream like that. – Mathijs Segers Mar 28 '17 at 06:32
  • @hasanzuav I did not find any help from your suggested link. – Humty Mar 28 '17 at 06:37
  • @MathijsSegers Any alternative? – Humty Mar 28 '17 at 06:37
  • `But Android gives the error. Permission denied for the attachment.`. No. That was not Android. Please retry. Tell exact. – greenapps Mar 28 '17 at 09:12
  • @greenapps See the picture in the question – Humty Mar 28 '17 at 09:32
  • Please just answer my question. That is not Android OS. What is it? Which app? – greenapps Mar 28 '17 at 09:33
  • @greenapps Which app?? I did not understand. I am working in android studio. Kindly run the above code. – Humty Mar 28 '17 at 09:36
  • Your app will run on an Android device isnt it? Has nothing to do with Android Studio. And you are trying to send an email. So which app is talking about permissions? – greenapps Mar 28 '17 at 09:43
  • @greenapps If i understand your question correctly, then It is gmail app in Samsun Glaxy android phone. An d I am running it on real device "Android 5" – Humty Mar 28 '17 at 09:48
  • Yes that message is from the user choosen email app of course. Not from Android OS. – greenapps Mar 28 '17 at 09:55
  • 1
    @Humty, what it your compile sdk, targetsdk and which device you are testing? I believe that you must be testing this on android api version 21 and above. Since permission modal changed for these api for reading storage so you need to handle that by asking these permission explicitly. – Geek Mar 29 '17 at 07:15
  • @Humty Try replacing startActivity with startActivityForResult. – Keyur Thumar Apr 10 '17 at 06:53
  • @KeyurThumar Now I am trying to do this using `FileProvider`. I am facing issue in it . Kindly see `http://stackoverflow.com/questions/43316201/cannot-attach-empty-file-in-gmail-using-file-provider?noredirect=1#comment73697247_43316201` – Humty Apr 10 '17 at 06:57

1 Answers1

0

You are trying to let the email app read a file from your app's private internal memory where only your app has access. Thats why the emai app complains.

You have at least two solutions.

First you could put your file on external storage where every app has access.

Second you could leave the file in private memory and use a FileProvider to serve the file.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • @greenaps Where can I save the file that every app has access? (Method 1) – Humty Apr 10 '17 at 04:11
  • `on external storage where every app has access.` – greenapps Apr 10 '17 at 11:40
  • I am using FileProvider on your suggestion and facing a problem in it. Kindly see http://stackoverflow.com/questions/43316201/error-cannot-attach-empty-file-in-gmail-app-using-file-provider – Humty Apr 10 '17 at 11:51