FileProvider Not Working with Camera

I’m trying to make Camera App to store the output to my internal storage. I also understand that third party apps are not able to access the Internal Storage of my application BUT we are able to do so by exposing the internal directory through FileProvider. I have followed the guide here:

I specify my AndroidManifest.xml in the following way:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    package="com.stackoverflow.test.camerawithfileprovider" >


            android:grantUriPermissions="true" >
                android:resource="@xml/file_provider_path" />


I created a xml file named file_provider_path.xml in /res/xml/:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="">
    <files-path name="image_capture" path="public/" />

This is how I create and call the Camera Intent:

private static final int CAMERA_REQUEST_CODE = 5000;
private static final String CAMERA_FP_AUTHORITY = "com.stackoverflow.test.camerawithfileprovider.fileprovider";

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

Context context = MainActivity.this;
File imagePath = new File(context.getFilesDir(), "public");
if (!imagePath.exists()) imagePath.mkdirs();
File newFile = new File(imagePath, "tmp.jpg");

Uri imageUri = FileProvider.getUriForFile(context, CAMERA_FP_AUTHORITY, newFile);
//context.grantUriPermission("", imageUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
Log.d("YouQi", "Image URI Passing to Camera: " + imageUri.toString());

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CAMERA_REQUEST_CODE);

When I run the application, I can get the below Logcat result:

    11-11 20:07:39.581 18303-18303/com.stackoverflow.test.camerawithfileprovider D/YouQi: Image URI Passing to Camera: content://com.stackoverflow.test.camerawithfileprovider.fileprovider/image_capture/tmp.jpg

Right at the moment when the camera closes, I hit the below Exception: E/AndroidRuntime: FATAL EXCEPTION: main E/AndroidRuntime: Process:, PID: 19420 E/AndroidRuntime: java.lang.SecurityException: Permission Denial: opening provider from ProcessRecord{42e44f70} (pid=19420, uid=10083) that is not exported from uid 10312 E/AndroidRuntime:     at android.os.Parcel.readException( E/AndroidRuntime:     at android.os.Parcel.readException( E/AndroidRuntime:     at E/AndroidRuntime:     at E/AndroidRuntime:     at$ApplicationContentResolver.acquireUnstableProvider( E/AndroidRuntime:     at android.content.ContentResolver.acquireUnstableProvider( E/AndroidRuntime:     at android.content.ContentResolver.openAssetFileDescriptor( E/AndroidRuntime:     at android.content.ContentResolver.openOutputStream( E/AndroidRuntime:     at android.content.ContentResolver.openOutputStream( E/AndroidRuntime:     at E/AndroidRuntime:     at$8.onClick( E/AndroidRuntime:     at android.view.View.performClick( E/AndroidRuntime:     at android.view.View$ E/AndroidRuntime:     at android.os.Handler.handleCallback( E/AndroidRuntime:     at android.os.Handler.dispatchMessage( E/AndroidRuntime:     at android.os.Looper.loop( E/AndroidRuntime:     at E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime:     at java.lang.reflect.Method.invoke( E/AndroidRuntime:     at$ E/AndroidRuntime:     at E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

Can anyone help to explain why the problem happen? I have created a sample project of my problem:

Anyway, the app will work if I explicitly specify:


But this will only work for nexus/cyanogen devices. Samsung camera app might use a different package name.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

  1. Please do not try to put exported="true" as pointed out by pskink, your app will crash the moment it loads. FileProvider is not meant to work in this state.

  2. Tried intent.addFlags solution by CommonsWare. Not working. probably will only works with ACTION_SEND kind of intent.

  3. I found the answer in this post. Apparently the manual granting way is the only solution. But we can always loop thru the list of candidate packages and grantPermission to all of them.

    List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    for (ResolveInfo resolveInfo : resInfoList) {
        String packageName = resolveInfo.activityInfo.packageName;
        context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);

Solution 2

I had a similar issue but later on I find out to have a typo in my code.
The typo was in AndroidManifest.xml, precisely into the tag at the line “authorities”.

For me,


doesn’t work (when I call the camera instance, the app crashes) but if i delete “file” and replace that string as following:


my app runs smoothly (the app takes the photo, stores it, and gives back a preview to the activity in the client app).

Does your app also have the same issue ?

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply