Home Error in method getUriForFile - java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/
Reply: 0

Error in method getUriForFile - java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/

J. Maruca
1#
J. Maruca Published in 2017-09-14 05:06:20Z

I've been working on a simple Android application that takes a picture and stores it in the app's directory. I've followed Android Developpers tutorials exactly but i'm still getting an error.

I've seen lots of questions about this error in StackOverflow, but none of them seen to fix my error.

This is the logcat error I get:

    FATAL EXCEPTION: main
Process: com.example.victormaruca.gpstesting, PID: 31539
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.victormaruca.gpstesting/files/Pictures/JPEG_20170914_014854_-551864997.jpg
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:712)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:401)
at com.example.victormaruca.gpstesting.MainActivity.dispatchTakePictureIntent(MainActivity.java:124)
at com.example.victormaruca.gpstesting.MainActivity.access$000(MainActivity.java:43)
at com.example.victormaruca.gpstesting.MainActivity$3.onClick(MainActivity.java:81)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21163)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

This is the method where I call getUriForFile:

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {

        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            Log.e("Erro", "no ceateImageFile() : "+ex);
        }

        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.victormaruca.gpstesting.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

And this is the method I create the file:

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 = image.getAbsolutePath();
    return image;
}

AndroidManifest.xml snippet:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.victormaruca.gpstesting.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>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


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

file_paths.xml :

  <?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.victormaruca.gpstesting/files/Pictures/" />
    </paths>

Any help would be apreciated!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.313159 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO