5

Just getting into the Glide image loading library for Android. Working with code from here: https://github.com/bumptech/glide/issues/459

Full project here: https://github.com/mhurwicz/glide02

I'm getting the following exception when I run the app in the emulator in Android Studio:

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.example.glide02/cache/image_manager_disk_cache/5a992029460eed14244e8b970d969d45518b2f7ac10f71eb26bd0aaf7c3bcf06.0

The rest of the messages are:

at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711)
                  at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
                  at com.example.glide02.ShareTask.onPostExecute(ShareTask.java:37)
                  at com.example.glide02.ShareTask.onPostExecute(ShareTask.java:15)
                  at android.os.AsyncTask.finish(AsyncTask.java:651)
                  at android.os.AsyncTask.-wrap1(AsyncTask.java)
                  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

The error occurs on FileProvider.getUriForFile()

@Override protected void onPostExecute(File result) {
    if (result == null) { return; }
    Uri uri = FileProvider.getUriForFile(context, context.getPackageName(), result);
    share(uri); // startActivity probably needs UI thread
}

I've looked at several other questions relating to this error, but can't see how they relate to my case, perhaps due to my lack of familiarity with this whole area.

Any help will be greatly appreciated.

This is the full code for the class in which the above method occurs:

class ShareTask extends AsyncTask<String, Void, File> {
private final Context context;

public ShareTask(Context context) {
    this.context = context;
}
@Override protected File doInBackground(String... params) {
    String url = params[0]; // should be easy to extend to share multiple images at once
    try {
        return Glide
                .with(context)
                .load(url)
                .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                .get() // needs to be called on background thread
                ;
    } catch (Exception ex) {
        Log.w("SHARE", "Sharing " + url + " failed", ex);
        return null;
    }
}
@Override protected void onPostExecute(File result) {
    if (result == null) { return; }
    Uri uri = FileProvider.getUriForFile(context, context.getPackageName(), result);
    share(uri); // startActivity probably needs UI thread
}

private void share(Uri result) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("image/jpeg");
    intent.putExtra(Intent.EXTRA_SUBJECT, "Shared image");
    intent.putExtra(Intent.EXTRA_TEXT, "Look what I found!");
    intent.putExtra(Intent.EXTRA_STREAM, result);
    context.startActivity(Intent.createChooser(intent, "Share image"));
}

}

user1147171
  • 1,213
  • 3
  • 14
  • 22

3 Answers3

16

In my case, this was the solution in filepath.xml

    <paths>
    <root-path name="root" path="." />
</paths>
  • 2
    I don'understand why but it works adding this line. Despite Android Studio says "Element 'root-path' is not allowed here". Mumble – kinghomer Sep 10 '20 at 15:13
9

In filepaths.xml you need

<cache-path name="shared_images_from_glide_cache" path="image_manager_disk_cache"/>

This will result in mapping

new File("/data/data/com.example.glide02/cache/image_manager_disk_cache/5a992029460eed14244e8b970d969d45518b2f7ac10f71eb26bd0aaf7c3bcf06.0")

to

Uri.parse("content://com.example.glide02/shared_images_from_glide_cache/5a992029460eed14244e8b970d969d45518b2f7ac10f71eb26bd0aaf7c3bcf06.0")

(Not sure if I got it exactly right, but you should get the point.)

TWiStErRob
  • 44,762
  • 26
  • 170
  • 254
  • That did it! Thank you so much @TWiStErRob! The project on github should now be functional, for any future searchers who may wish to reference it. – user1147171 Jan 20 '17 at 20:23
  • Hi @TWiStErRob, I am trying to access file under System folder. Can you please help me what be needed to add in filepaths.xml file.?I am getting this exception : Failed to find configured root that contains /system/CSCVersion.txt.Thanks – VP4Android Dec 04 '18 at 22:02
  • 1
    i used `File file = new File(activity.getCacheDir(), "WireInstructions.pdf");` and `uri = FileProvider.getUriForFile(activity, activity.getApplicationContext().getPackageName() + ".provider", file);` with ` ` and it worked perfectly! – abdulwasey20 Mar 01 '21 at 10:01
5

When I was trying to get Uri for the file stored in an external SD card, I was getting this error. but when I do the same set of operations with files stored in internal storage, it worked fine. In my case making the below-mentioned changes in the resource file which is @xml/file_path(you can check yours under meta-data tag of the provider in the manifest.)

//adding the below line will give you a warning saying that Element root-path is not allowed here
//but you can ignore that.
<paths>
<root-path name="root" path="." />
</paths

The below code is for reference.

//@xml/file_paths.xml

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

//My provider in Manifest.xml
<provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

Tested with android 11, 10 and 7

Vishal Naikawadi
  • 419
  • 6
  • 11
  • This is the same answer as Atef Farouk 15 June 2020, but helpfully adds the entire file.xml contents. – Dale Aug 12 '22 at 20:10