简体   繁体   中英

permission denied for attachment gmail android

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[]
                                {"examplegmail@gmail.com"});
                        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..."));


            }
        });


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM