Skip to content Skip to sidebar Skip to footer

Fileuriexposedexception In Android N With Camera

MainActivity import android.Manifest; import android.app.Activity; import android.app.AlertDialog; import android.content.ContentResolver; import android.content.ContentValues; imp

Solution 1:

I have also faced the same issue in Android N devices. But i resolved it.

Here is my code which can solve the issue:

publicvoidlaunchCamera() {
    Intentintent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
        intent.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri());
    } else {
        Filefile=newFile(getPhotoFileUri().getPath());
        UriphotoUri= FileProvider.getUriForFile(getApplicationContext(), getApplicationContext().getPackageName() + ".provider", file);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
    }
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    if (intent.resolveActivity(getApplicationContext().getPackageManager()) != null) {
        startActivityForResult(intent, REQUEST_CAMERA);
    }
}

After this you need to create a XML folder in res, and in that folder you need to create a xml labeled provider_paths.xml

Code in provider_paths.xml

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

Later in manifest you need to add the following inside the application tag and make sure that the compileSdkVersion >=24

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

Here are the two reference links which can guide you better for better understanding.

Links:

link1 - from medium cooperation

link2 - from inthecheesefactory

Solution 2:

If you've are not touching other apps in the system just put

StrictMode.VmPolicy.Builderbuilder=newStrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());

in your Application.onCreate(). It'll ignore the URI exposure and you are good to go.

Solution 3:

You can use StrictMode, add below code to your Activity onCreate()

    StrictMode.VmPolicy.Builderbuilder=newStrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());

Solution 4:

From Android N, android has changed the way you provide a file URI. Using file:// uri is prohibited and would throw this. Use the FileProvider to overcome this.

Passing file:// URIs outside the package domain may leave the receiver with an unaccessible path. Therefore, attempts to pass a file:// URI trigger a FileUriExposedException. The recommended way to share the content of a private file is using the FileProvider.

More info can be found here

Solution 5:

change ur buildToolsVersion

 buildToolsVersion "25.0.1"

to

 buildToolsVersion "23.0.1"

Post a Comment for "Fileuriexposedexception In Android N With Camera"