Android:无法添加附件:openAssetFileDescriptor时出现SecurityException

时间:2019-01-17 15:14:40

标签: java android

我无法将文件附加到本机android电子邮件或Gmail应用程序中。

Gmail给我这个错误:


2019-01-17 16:33:17.884 15415-15415/? E/Gmail: Gmail:Error adding attachment
    fjd: SecurityException when openAssetFileDescriptor.

这是将文件保存到存储中的代码:

 public Uri getUrlFromDrawable(String base64ImageData) {

        FileOutputStream fos;
        try {

          final String pureBase64Encoded = base64ImageData.substring(base64ImageData.indexOf(",") + 1);
          final byte[] decodedBytes = Base64.decode(pureBase64Encoded, Base64.DEFAULT);
          String filename = "receipt2_" + System.currentTimeMillis() + ".jpg";
          File file = new File(reactContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES), filename);
          fos = new FileOutputStream(file);
          fos.write(decodedBytes);
          fos.flush();
          fos.close();

        Uri contentUri = FileProvider.getUriForFile(getReactApplicationContext(), "com.myapp.fileprovider", file);
        return contentUri;
    }


return null
}

这是我发送邮件的功能:

 public void mail(ReadableMap options, Callback callback) {
        Intent i = new Intent(Intent.ACTION_SENDTO);
        i.setData(Uri.parse("mailto:"));

        if (options.hasKey("subject") && !options.isNull("subject")) {
            i.putExtra(Intent.EXTRA_SUBJECT, options.getString("subject"));
        }

        if (options.hasKey("body") && !options.isNull("body")) {
            String body = options.getString("body");
            i.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(new StringBuilder().append(body).toString())
            );

        }

        if (options.hasKey("recipients") && !options.isNull("recipients")) {
            ReadableArray recipients = options.getArray("recipients");
            i.putExtra(Intent.EXTRA_EMAIL, readableArrayToStringArray(recipients));
        }

        if (options.hasKey("receipt") && !options.isNull("receipt")) {
            String base64ImageData = options.getString("receipt");

            try {
                if (base64ImageData != null) {
                    Uri imageUri = getUrlFromDrawable(base64ImageData);
                    i.putExtra(Intent.EXTRA_STREAM, imageUri);

                }

            } catch (Exception e) {
                Log.e("ERROR", "Cannot save receipt");
            }


        }

        PackageManager manager = reactContext.getPackageManager();
        List<ResolveInfo> list = manager.queryIntentActivities(i, 0);


        if (list == null || list.size() == 0) {
            callback.invoke("not_available");
            return;
        }

        if (list.size() == 1) {
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            try {
                reactContext.startActivity(i);
            } catch (Exception ex) {
                callback.invoke("error");
            }
        } else {
            Intent chooser = Intent.createChooser(i, "Send Mail");
            chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            try {
                reactContext.startActivity(chooser);
            } catch (Exception ex) {
                callback.invoke("error");
            }
        }
    }

这是fileprovider.xml

   <?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-files-path
        name="Pictures"
        path="/" />
</paths>

这是`AndroidManifest.xml

中的provider
<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.myapp.fileprovider"
            tools:replace="android:authorities"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                tools:replace="android:resource"
                android:resource="@xml/fileprovider" />
        </provider>

2 个答案:

答案 0 :(得分:2)

如果您也将其添加为 ClipData,则权限授予将成功:

yourIntent.clipData = ClipData.newUri(context.contentResolver, fileName, contentUri)

罪魁祸首是 Intent.FLAG_GRANT_READ_URI_PERMISSION 仅适用于 Intent.dataIntent.clipData,而不适用于额外的,除非已明确授予 uri 权限(不方便),或已授予还添加一个 ClipData

请注意,your answer 也有效,但我发现它不太方便,因为对于 Android 11+,它要求您在 AndroidManifest.xml 中声明查询,这在某些情况下也可能是隐私问题案例。

答案 1 :(得分:0)

我设法通过在运行时授予权限来解决此问题

  List<ResolveInfo> list =  manager.queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo resolveInfo : list) {
            String packageName = resolveInfo.activityInfo.packageName;
            reactContext.grantUriPermission(packageName, imageUri , Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }