2

I'm trying to send an email with an attached file. The file in internal storage, so this is my code:

File filelocation = new File(getFilesDir().getAbsolutePath()+"/MyApp", "FileName");
        Uri path = Uri.fromFile(filelocation);
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent .setType("vnd.android.cursor.dir/email");
        String to[] = {"mailmailmail@gmail.com"};
        emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
        emailIntent .putExtra(Intent.EXTRA_STREAM, path);
        emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
        startActivity(Intent.createChooser(emailIntent , "Send email..."));

But I obtain always: Permission denied for file.

How can I solve it??

Droide
  • 1,807
  • 2
  • 17
  • 30
  • GMail 5.0 only accept files from external storage http://stackoverflow.com/questions/26883259/gmail-5-0-app-fails-with-permission-denied-for-the-attachment-when-it-receives – L.Velazquez Jul 06 '16 at 22:15

2 Answers2

4

I solved in this way: I copy the file to send, into external cache dir, and send it.

File temporaryFile = null;
    try {
        temporaryFile = File.createTempFile(keyType.getKeyTypeString(), ".pem", context.getExternalCacheDir() );
        Utils.copy(new File(getFilesDir().getAbsolutePath()+"/"+ Utils.APP_OPERATOR_DIR, keyType.getKeyTypeString()+".pem"), temporaryFile);
    } catch (IOException e) {
        e.printStackTrace();
    }

File filelocation = new File(getFilesDir().getAbsolutePath()+"/MyApp", "FileName");
        Uri path = Uri.fromFile(filelocation);
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent .setType("vnd.android.cursor.dir/email");
        String to[] = {"mailmailmail@gmail.com"};
        emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
        emailIntent .putExtra(Intent.EXTRA_STREAM, path);
        emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
        startActivity(Intent.createChooser(emailIntent , "Send email..."));
Droide
  • 1,807
  • 2
  • 17
  • 30
3

-Make sure you have added the read permissions in the manifest

uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE".

-GMail 5.0 only accept files from external storage Gmail 5.0 app fails with "Permission denied for the attachment" when it receives ACTION_SEND intent.

Also you can use this library: compile'com.github.yesidlazaro:GmailBackground:1.1'.

    String imagePath = data.getStringExtra(GOTOConstants.IntentExtras.IMAGE_PATH);

    BackgroundMail.newBuilder(ReportBugActivity.this)
            .withUsername("some_email@gmail.com")
            .withPassword("pages123")
            .withMailto("mail_to_email.bugs@gmail.com")
            .withSubject("Android Bug Report")
            .withAttachments(imagePath)
            .withBody("Android Bug Report")
            .withOnSuccessCallback(new BackgroundMail.OnSuccessCallback() {
                @Override
                public void onSuccess() {
                    Toast.makeText(getApplicationContext(), "Email Sent", Toast.LENGTH_LONG).show();

                    finish();
                    startActivity(getIntent());
                }
            })
            .withOnFailCallback(new BackgroundMail.OnFailCallback() {
                @Override
                public void onFail() {
                    Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_LONG).show();
                }
            }).send();
Community
  • 1
  • 1
L.Velazquez
  • 1,696
  • 1
  • 11
  • 7