1

I'm having issues with File Provider. I'm trying to get the Uri of an image I take with my phone camera. But whenever I try taking a photo, it's giving me the following error:

FATAL EXCEPTION: main Process: com.example.android.inventory, PID: 30523 
java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.example.android.inventory/cache/IMG_20170718_213454_2102974580.jpg
                                                                               at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:679)
                                                                               at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:378)
                                                                               at com.example.android.inventory.EditorActivity$5.onClick(EditorActivity.java:239)                                                                 

onClickListener

//press button to take a photo
    final Button addImage = (Button) findViewById(R.id.click);
    addImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            try {
                File f = createImageFile();

                Log.d(LOG_TAG, "File: " + f.getAbsolutePath());

                mImageUri = FileProvider.getUriForFile(
                        EditorActivity.this, FILE_PROVIDER_AUTHORITY, f);

                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);

                if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    });

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name="com.example.android.inventory.CatalogActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.android.inventory.EditorActivity"
        android:theme="@style/EditorTheme"
        android:parentActivityName="com.example.android.inventory.CatalogActivity" >
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.android.inventory.CatalogActivity" />
    </activity>
    <provider
        android:name="com.example.android.inventory.data.InventoryProvider"
        android:authorities="com.example.android.inventory"
        android:exported="false" />

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.android.myfileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_provider_paths" />
    </provider>
</application>

<uses-feature android:name="android.hardware.camera"
    android:required="true" />

File provider Path

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

Any help would be appreciated. Thank you.

Shreeya Chhatrala
  • 1,441
  • 18
  • 33
fokusfocus
  • 195
  • 1
  • 3
  • 15
  • https://stackoverflow.com/questions/42516126/fileprovider-illegalargumentexception-failed-to-find-configured-root – sHOLE Jul 19 '17 at 04:43

3 Answers3

0

You can do it alternatively using Bitmap.

//The method below is opened when button is clicked you can handle in your own way 
    private void OpenImageChooser() {
                Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
//on Activity result will give you the data when you can convert it into bitmap to show in the image view
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
                Bundle extras = data.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.get("data");
                imgAttendance.setImageBitmap(imageBitmap);
            }
        }
    }
Pratik Vyas
  • 644
  • 7
  • 20
  • This is only for thumbnail because of bitmap quality. If you need fully size image, you should save it. – eL_ Jan 21 '20 at 09:43
0

In AndroidMenifest.xml

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

provider_paths.xml

<paths xmlns:android="http://schemas.android.com/apk/res/android">
       <external-path name="external_files" path="."/>
</paths>

In OnclickListener after file create

if (Build.VERSION.SDK_INT >= 24)
   mImageCaptureUri = FileProvider.getUriForFile(EditorActivity.this, BuildConfig.APPLICATION_ID + ".provider", f);
else
   mImageCaptureUri = Uri.fromFile(f);
Shweta Chauhan
  • 6,739
  • 6
  • 37
  • 57
0

Here is code snippet to get Uri from file provider.

File imageFile = createImageFile();


picUri = FileProvider.getUriForFile(RegisterActivity.this , this.getApplicationContext().getPackageName() + ".provider", imageFile);

Below is file provider path

provider_paths.xml

file.

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

Here <external-path> define that image save in external storage

And below shows how to define file provide in Manifest file.

<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>
Akash Ratanpara
  • 240
  • 1
  • 2
  • 14