huey77
huey77

Reputation: 683

Taking photo and saving to internal storage and getting error for getUriForFile

So I'm working on an app where I'm taking a photo and trying to save it to the apps internal storage. I'm having issues with the fileprovider. I have looked at many many of the questions asked on stack overflow, but would like to get a more in detail explanation if possible.

I also followed googles example and it gives me the following error. https://developer.android.com/training/camera/photobasics

Failed to find configured root that contains /storage/emulated/0/Android/data/com.myapp.debug/files/Pictures/JPEG_20180427_095752_2090822261.jpg

Here is the code i have whenever i follow Google's example.

<provider
        android:name=".application.blinkupregistration.postworkphotos.PostWorkPhotosFileProvider"
        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>

In my code.

Uri photoURI = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", photoFile);

For the two above, I also tried to hardcode com.myapp.provider into the authorities and getUriForFile method. Also did getpackageName() for the getUriForFile method. But these did not change much. I think the main issue is the paths.

Tried the following paths by using Google's example,

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="post_work_photos" path="Android/data/${applicationId}/files/Pictures" />
</paths>


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

I get this to work whenever I change my paths.xml to the following.

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

But I do not understand why it works with the period. I also do not know if this the correct practice, which is my main concern.

If anyone can help me out, then that would be great. Thanks.

Upvotes: 2

Views: 821

Answers (2)

user9566632
user9566632

Reputation:

If you are trying to save a picture to your app's data folder, this should work:

Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File image = new File(getApplicationContext().dataDir, "image.jpg");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

context.dataDir is supported since API level 4.

Full path to the default directory assigned to the package for its persistent data.

Otherwise, if you are working above above API level 24, you can call getFilesDir() anywhere in context. (Activity, Service, ...)

Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006869

Tried the following paths by using Google's example,

Their example... has issues.

But I do not understand why it works with the period.

Because you are saying that you are willing to support anything inside of the root of external storage.

I also do not know if this the correct practice

You could reduce the scope a bit by using <external-files-path>:

<external-files-path name="post_work_photos" path="." />

Now, you are only serving files out of getExternalFilesDir(), which is where your file points to.

Upvotes: 1

Related Questions