拍照时获取 - java.lang.Throwable: file://Uri 通过 ClipData.Item.getUri() 暴露 [英] when take photo get - java.lang.Throwable: file:// Uri exposed through ClipData.Item.getUri()

查看:20
本文介绍了拍照时获取 - java.lang.Throwable: file://Uri 通过 ClipData.Item.getUri() 暴露的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例外是:

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);
}

我搜索了类似的问题和解决方案.并修改代码如下:

I searched the similar problems and solutions. And modify the code as follow:

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 上运行良好.有没有人遇到同样的问题?要求一些预付款.在线等待...

It happend on Android 5.1 While work well on Android 4.3. Is there anyone meet the same problem? Ask for some advance. Waiting online...

推荐答案

我已经解决了这个问题.

I have already resolved this problem.

首先,出现此问题是因为 StrictMode 阻止使用 file:// 方案传递 URI.

First, this problem occurred because StrictMode prevents passing URIs with a file:// scheme.

所以有两种解决方案:

  1. 更改 StrictMode.请参阅类似问题它的代码.但是对于我们的应用来说,修改安卓源代码是不现实的.

  1. Change StrictMode. See similar problem and its code. But for our apps, it is not realistic to modify the Android source code.

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

Use another URI scheme, instead of file://. For example, content:// related to MediaStore.

所以我选择了第二种方法:

So I chose the second method:

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);
}

另外,还有另一个解决方案.

这篇关于拍照时获取 - java.lang.Throwable: file://Uri 通过 ClipData.Item.getUri() 暴露的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆