【问题标题】:when take photo get - java.lang.Throwable: file:// Uri exposed through ClipData.Item.getUri()拍照时获取 - java.lang.Throwable: file:// Uri 通过 ClipData.Item.getUri() 暴露
【发布时间】:2015-12-13 21:47:38
【问题描述】:

例外是:

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

【问题讨论】:

    标签: android camera uri mediastore image-capture


    【解决方案1】:

    我已经解决了这个问题。

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

    所以有两种解决方案:

    1. 更改StrictMode。请参阅 similar problemits code。 但是对于我们的应用来说,修改Android源代码是不现实的。

    2. 使用另一个 URI 方案,而不是 file://。例如,content://MediaStore 相关。

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

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

    另外,还有一个solution

    【讨论】:

    • "...需要 android.permission.WRITE_EXTERNAL_STORAGE 或 grantUriPermission()"
    【解决方案2】:

    所以,我实际上是在阅读这方面的内容,似乎处理此问题的正确解决方案如下:

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

    请注意,Google 说要创建一个“content://”文件,而不是基于“file://”的资源。

    这是来自谷歌:

    Note: We are using getUriForFile(Context, String, File) which returns a content:// URI. For more recent apps targeting Android N and higher, passing a file:// URI across a package boundary causes a FileUriExposedException. Therefore, we now present a more generic way of storing images using a FileProvider.

    此外,您还需要设置以下内容: Now, you need to configure the FileProvider. In your app's manifest, add a provider to your application:

    <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>
    

    注意:(取自谷歌网站)Make sure that the authorities string matches the second argument to getUriForFile(Context, String, File). In the meta-data section of the provider definition, you can see that the provider expects eligible paths to be configured in a dedicated resource file, res/xml/file_paths.xml. Here is the content required for this particular example:

    <?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

    【讨论】:

      【解决方案3】:

      除了使用 FileProvider 的解决方案之外,还有另一种解决方法。简单地放在 Application.onCreate() 方法中。这样VM就忽略了文件URI暴露。

      StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
       StrictMode.setVmPolicy(builder.build());
      

      【讨论】:

      • 你能解释一下这到底是做什么的吗?
      • 我不知道它在做什么,但解决了我的问题。谢谢
      【解决方案4】:

      这个错误的原因是 file:// uri 方案不再支持,因为安全性暴露。 https://code.google.com/p/android/issues/detail?id=203555

      在使用 targetSDK 'N' 之后,我们不能再使用 file:// uri。 https://commonsware.com/blog/2016/03/14/psa-file-scheme-ban-n-developer-preview.html

      所以,答案是正确的。任何使用 file:// 的人都可以更改 content:// 以提供各种本地文件。

      【讨论】:

      • 谢谢,我将targetSdk 降级为23,它已经开始工作了。我的意思是File.getUriFrom(File file)
      【解决方案5】:

      总结一下: file:// 方案现在不允许在 targetSdkVersion 24 (Android Nougat) 上附加 Intent

      如果您计划支持 api 24+,则必须更改您的代码 两个链接: https://developer.android.com/training/camera/photobasics.html https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en

      【讨论】:

      • 好一个,真的很有帮助
      猜你喜欢
      • 1970-01-01
      • 2018-06-15
      • 2017-07-04
      • 2018-08-06
      • 1970-01-01
      • 2015-11-27
      • 2011-06-30
      • 1970-01-01
      相关资源
      最近更新 更多