30 dic

getUriForFile() throws Failed to find configured root that contains

I'm making an app and I need to take a picture and then save some data on the database, but i'm getting this error when i try to get the URI from the photo:

java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/br.com.rfcgraciano.fotoaula/files/Pictures/JPEG_20160915_161210_-695228406.jpg

Here's my code

package br.com.rfcgraciano.fotoaula;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class PicturesActivity extends AppCompatActivity {

    private static final String TAG = "PicturesActivity";
    private static final int REQUEST_IMAGE_CAPTURE = 1;
    private FloatingActionButton fabNewPicture;
    private String mCurrentPhotoPath;
    private ImageView imageView;

    protected void onCreate(Bundle savedInstanceState)

        imageView = (ImageView)findViewById(R.id.imageView);
        fabNewPicture = (FloatingActionButton)findViewById(R.id.fab_new_picture);
        fabNewPicture.setOnClickListener(new View.OnClickListener()
            public void onClick(View v) {

    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 = "file:" + image.getAbsolutePath();
        return image;

    private void dispatchTakePictureIntent()
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                Toast.makeText(getApplicationContext(), "Erro ao salvar imagem!", Toast.LENGTH_SHORT).show();
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Toast.makeText(this, "Imagem capturada com sucesso, salvar dados no BD", Toast.LENGTH_SHORT).show();

My provider:


And my 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/br.com.rfcgraciano.fotodata/files/Pictures" />

I did it exactly as the documentation, can you guys help me to find a solution?

Thank you very much!



Step #1: Switch to the latest version of the support libraries (24.2.1 as of 15 September 2016)

Step #2: Change your file_paths.xml to:

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

and see if you have better luck.


Picture path should be finished with slash.

Android/data/br.com.rfcgraciano.fotodata/files/Pictures ko Android/data/br.com.rfcgraciano.fotodata/files/Pictures/ ok

Let me know if it will work out.


thank you CommonsWare it worked!


© 2017 website by Rubit Corporation