Android camera intent FileUriExposedException for SDK >= 24

后端 未结 2 604
小鲜肉
小鲜肉 2021-02-05 16:11

I use this code to get a picture from camera and put it on imageview:

  private void openCamera()
{
    mMediaUri =getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

             


        
相关标签:
2条回答
  • 2021-02-05 16:39

    Instead of return Uri.fromFile(mediaFile); do

    return FileProvider.getUriForFile(MainActivity.this,
        BuildConfig.APPLICATION_ID + ".provider",
        mediaFile);
    

    That would require you to add a provider to the AndroidManifest:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    <application
        ...
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>
    </application>
    

    For AndroidX, use androidx.core.content.FileProvider

    And then create a provider_paths.xml file in xml folder under res folder.

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <external-path name="external_files" path="."/>
    </paths>
    

    Read more: Full article

    0 讨论(0)
  • 2021-02-05 16:42

    It can be done as simple as below . Put these 2 lines in onCreate of your activity.

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

    URI exposure will be ignored by Vm I resolved my issue so.

    0 讨论(0)
提交回复
热议问题