【问题标题】:permission denied for attachment gmail android附件gmail android的权限被拒绝
【发布时间】:2022-01-02 14:03:25
【问题描述】:

当我按下按钮时,我想通过邮件将 json 文件发送到设备内部。当我切换到 Gmail 端时,我收到错误“附件的权限被拒绝”。我该如何解决这个问题?

清单:

    <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" />

我的代码;


 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..."));


            }
        });


【问题讨论】:

    标签: java android json android-intent gmail


    【解决方案1】:

    因为电子邮件应用无权访问您目录结构中的文件。这就是你正在做的事情——启动另一个应用程序并传递文件名。您需要为此使用 FileProvider,请参阅https://developer.android.com/reference/androidx/core/content/FileProvider

    【讨论】:

    • 谢谢。我用 FiloProvider 解决了这个问题
    猜你喜欢
    • 1970-01-01
    • 2013-07-03
    • 2015-12-21
    • 2020-06-03
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    • 2023-03-03
    相关资源
    最近更新 更多