Android 10+ pdf 没有发送到 Gmail

Android 10+ pdf doesnt send to Gmail

我的 phone 上有文件。我无法将 pdf 文件发送到 Gmail,为了进行测试,我创建了一个包含 png 和 pdf 文件的列表,这些文件位于同一文件夹中。但是,png 文件被发送到 Gmail,pdf 文件没有附加。不明白为什么会这样

我的代码:

Intent emailIntent = new Intent(ACTION_SEND_MULTIPLE);

     

            String theme = "Test";

            emailIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            emailIntent.setType(" message/rfc822"); 

            String to[] = {sendTo};
            emailIntent.putExtra(EXTRA_EMAIL, to);

            emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
            emailIntent.putExtra(EXTRA_SUBJECT, theme);

           emailIntent.putExtra(EXTRA_TEXT, context.getResources().getString(R.string.text_email_message_body));
try {
            ((Activity) context).startActivity(Intent.createChooser(emailIntent, "Choose an Email client:"));
        }catch (Exception e){
        }

更新:

此代码适用于 android 9 和 8

更新:

我添加了这样的文件:

       ArrayList<Uri> uris = new ArrayList<>();

 File fileIn1 = new File("/storage/emulated/0/TestApp/TestFolder/201123192703_1.png");
        Uri u1 = Uri.fromFile(fileIn1);

        File fileIn = new File("/storage/emulated/0/TestApp/TestFolder/201124171209_1.pdf");
        Uri u = Uri.fromFile(fileIn);

        File fileIn2 = new File("/storage/emulated/0/TestApp/TestFolder/201124171209_2.pdf");
        Uri u2 = Uri.fromFile(fileIn2);

        File fileIn3 = new File("/storage/emulated/0/TestApp/TestFolder/test.pdf");
        Uri u3 = Uri.fromFile(fileIn3);

        uris.add(u);
        uris.add(u2);
        uris.add(u1);
        uris.add(u3);

emailIntent.setType(" message/rfc822"); 替换为 emailIntent.setType("application/pdf")

file: Uri 值自 Android 7.0 以来已被禁止,着眼于您在 Android 10+ 中看到的各种“范围存储”限制.通常,当您尝试在 Intent 中使用 file: Uri 时,您会因 FileUriExposedException 而崩溃 — 我不确定为什么您不会在这里崩溃。总的来说,当把一个Uri放在一个Intent中时,它需要使用一个content:方案,而获取其中一个文件的典型方法是通过FileProvidergetUriForFile().