Permission denied for attachment gmail android

0

When I press the button, I want to send the json file inside the device via mail. When I switch to the Gmail side, I get the error "permission denied for attachment". How can I solve this problem?

manifest:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

my code;


 sendgmailButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String filepath = "/data/data/com.example.newgen/files/jsonexample.json";


                        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                        emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        emailIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                        emailIntent.setType("application/json");
                         emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]
                                {"[email protected]"});
                        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                                "Test Subject");
                        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                                "go on read the emails");
               
                        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+ filepath));


        
                        startActivity(Intent.createChooser(emailIntent, "Send mail..."));


            }
        });


android android-intent gmail java
2021-11-24 06:47:21
1

0

Because the email app doesn't have permission to access a file in your directory structure. And that's what you're doing- launching another app and passing the filename over. You need to use a FileProvider for this, see https://developer.android.com/reference/androidx/core/content/FileProvider

2021-11-24 06:51:53

thank you. I solved the problem with FiloProvider
Muratcan Yıldız

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................