在拍照时出现了 - java.lang.Throwable: file:// Uri exposed through ClipData.Item.getUri()

24

异常是:

file:// Uri exposed through ClipData.Item.getUri()
java.lang.Throwable: file:// Uri exposed through ClipData.Item.getUri()
    at android.os.StrictMode.onFileUriExposed(StrictMode.java:1618)
    at android.net.Uri.checkFileUriExposed(Uri.java:2341)
    at android.content.ClipData.prepareToLeaveProcess(ClipData.java:808)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:7926)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1506)
    at android.app.Activity.startActivityForResult(Activity.java:3832)
    at android.app.Activity.startActivityForResult(Activity.java:3783)
    at android.support.v4.app.FragmentActivity.startActivityFromFragment(Unknown Source)
    at android.support.v4.app.Fragment.startActivityForResult(Unknown Source)
    at me.chunyu.ChunyuDoctor.Utility.w.takePhoto(Unknown Source)
    at me.chunyu.ChunyuDoctor.Dialog.ChoosePhotoDialogFragment.takePhoto(Unknown Source)
    at me.chunyu.ChunyuDoctor.Dialog.ChoosePhotoDialogFragment.access$000(Unknown Source)
    at me.chunyu.ChunyuDoctor.Dialog.b.onClick(Unknown Source)
    at me.chunyu.ChunyuDoctor.Dialog.ChoiceDialogFragment.onClick(Unknown Source)
    at android.view.View.performClick(View.java:4848)
    at android.view.View$PerformClick.run(View.java:20270)
    at android.os.Handler.handleCallback(Handler.java:815)
    at android.os.Handler.dispatchMessage(Handler.java:104)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5643)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

我的代码在这里:

public static void takePhoto(Fragment fragment, int token, Uri uri) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (uri != null) {
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    }
    fragment.startActivityForResult(intent, token);
}

我搜索了类似的问题和解决方案,然后按照以下方式修改了代码:

public static void takePhoto(Fragment fragment, int token, Uri uri) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
            | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    if (uri != null) {
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
    }
    fragment.startActivityForResult(intent, token);
}

但它也不工作。

这在Android 5.1上发生,而在Android 4.3上正常工作。 有没有人遇到同样的问题? 寻求一些进展。 在线等待...

5个回答

15

我已经解决了这个问题。

首先,这个问题是因为StrictMode阻止传递带有file://方案的URI所导致的。

因此,有两种解决方法:

  1. 更改StrictMode。请参见类似问题其代码。但是对于我们的应用程序来说,修改Android源代码并不现实。

  2. 使用其他URI方案,而不是file://。例如,与MediaStore相关的content://

因此,我选择了第二种方法:

private void doTakePhoto() {
    try {
        ContentValues values = new ContentValues(1);
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
        mCameraTempUri = getActivity().getContentResolver()
                .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

        takePhoto(this, RequestCode.REQCODE_TAKE_PHOTO, mCameraTempUri);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void takePhoto(Fragment fragment, int token, Uri uri) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
        | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    if (uri != null) {
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
    }
    fragment.startActivityForResult(intent, token);
}

另外,还有另一种解决方案


需要 android.permission.WRITE_EXTERNAL_STORAGE 权限,或者授予 grantUriPermission()。 - ban-geoengineering

12

所以,我实际上是在研究这个问题,并且似乎正确的解决方案如下:

Therefore, I was actually reading about this, and it seems the correct solution to handle this is the following:

String mCurrentPhotoPath;

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

static final int REQUEST_TAKE_PHOTO = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            ...
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                                                  "com.example.android.fileprovider",
                                                  photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

请注意,谷歌建议创建“content://”文件而不是基于“file://”的资源文件。

这是来自Google的说明:

注意:我们正在使用getUriForFile(Context,String,File),它返回一个content:// URI。对于针对Android N及更高版本的较新应用程序,跨包边界传递file:// URI会导致FileUriExposedException。因此,现在我们提供了一种更通用的方式来使用FileProvider存储图像。

此外,您需要设置以下内容:现在,您需要配置FileProvider。在应用程序清单中,为应用程序添加提供程序:

<application>
   ...
   <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.android.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"></meta-data>
    </provider>
    ...
</application>
注意:(摘自Google网站)确保 authorities 字符串与第二个参数 getUriForFile(Context, String, File) 匹配。在提供程序定义的元数据部分中,您可以看到提供程序希望配置为合格路径的资源配置在一个专用资源文件 res/xml/file_paths.xml 中。以下是此特定示例所需的内容:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="Android/data/com.example.package.name/files/Pictures" />
</paths>

如果您想获取更多信息:请在此处阅读 https://developer.android.com/training/camera/photobasics.html


5
除了使用FileProvider解决方案之外,还有另一种方法可以解决这个问题。只需要在Application.onCreate()方法中加入代码即可。通过这种方式,虚拟机将忽略文件URI的暴露。
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
 StrictMode.setVmPolicy(builder.build());

请问您能否解释一下这个代码的具体作用? - HB.
我不知道它在做什么,但解决了我的问题。谢谢。 - BharathRao

4

1
谢谢,我将targetSdk降级到23,现在它可以工作了。我的意思是File.getUriFrom(File file) - murt

3

很好,真的很有帮助。 - Mohit Dixit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接