Ronn Ronn -4 years ago 428
Android Question

Getting Failed to find configured root that contains /storage/emulated/0/Pictures/NoteTaking/IMG_20170226_231007.jpg while trying to take a photo

I am trying to make an app that takes a photo and displays it using emulator instead of a device. I followed the steps from this android doc:

Part 1) Here is my code where it gets stucks :

file = FileProvider.getUriForFile(this, "edu.android.notetakingapplication.provider", createFileDir());


This part is in the Mainxml

public void takePicture(View view){
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = FileProvider.getUriForFile(this, "edu.android.notetakingapplication.provider", createFileDir());
String s = file.getPath();
Log.v("File Path","File path is"+s);
takePicture.putExtra(MediaStore.EXTRA_OUTPUT,file);
startActivityForResult(takePicture,100);
}
private static File createFileDir(){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "NoteTaking");

if (!mediaStorageDir.exists()){
if (!mediaStorageDir.mkdirs()){
Log.d("NoteTaking", "failed to create directory");
return null;
}
}

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
return new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
}


Here is my app manifest file's provider section

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

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="edu.android.notetakingapplication.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>


and provider_paths.xml

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


Part 2) If i keep provider_paths as this

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


I am able to open camera app, take photo and view it on the same page, but, here is the catch, I am storing the path into database and trying to retrieve all the images on a page. Here is when it gives an error:

Unable to decode stream: java.io.FileNotFoundException: /external_files/Pictures/NoteTaking/IMG_20170226_230608.jpg (No such file or directory)

Answer Source

Try this surely it will work:

Uri mImageCaptureUri;
private void operCamera() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
            "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));

    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

    intent.putExtra("return-data", true);

    startActivityForResult(intent, 1);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && mImageCaptureUri != null && resultCode != 0) {
        if (mImageCaptureUri != null) {
            String path1 = mImageCaptureUri.getPath();
            if (path1 != null) {
                File file1 = new File(path1);
                Uri capturedUri = Uri.fromFile(file1);//here you get the URI
                //you can easily get the path from URI if you need
            }
        }
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download