when take photo get - java.lang.Throwable: file:// Uri exposed through ClipData.Item.getUri()

后端 未结 5 1516
灰色年华
灰色年华 2020-12-05 10:49

The Exception is:

file:// Uri exposed through ClipData.Item.getUri()
java.lang.Throwable: file:// Uri exposed through ClipData.Item.getUri()
    at android.         


        
5条回答
  •  萌比男神i
    2020-12-05 11:22

    I have already resolved this problem.

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

    So there are two solutions:

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

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

    Also, there is another solution.

提交回复
热议问题