Hot questions for Using Glide in android fileprovider

Top 10 Java Open Source / Glide / android fileprovider


Just getting into the Glide image loading library for Android. Working with code from here:

Full project here:

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 com.example.glide02.ShareTask.onPostExecute(
                  at com.example.glide02.ShareTask.onPostExecute(
                  at android.os.AsyncTask.finish(
                  at android.os.AsyncTask.-wrap1(
                  at android.os.AsyncTask$InternalHandler.handleMessage(
                  at android.os.Handler.dispatchMessage(
                  at android.os.Looper.loop(
                  at java.lang.reflect.Method.invoke(Native Method)

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
                .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.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"));



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")



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


I'm attempting to use the native Android share functionality to share a set of images I retrieved using Glide. So any app that is compatible with the images will show up in Android's share menu.

I created a method that gets invoked when share is clicked.

private void onShareClicked() { 
    GlideUrl glideUrl = new GlideUrl(url1, new LazyHeaders.Builder().addHeader("x-auth-token", mToken).build());
    FutureTarget<File> image1 = mGlide.load(glideUrl).downloadOnly(SIZE_ORIGINAL, SIZE_ORIGINAL);

    glideUrl = new GlideUrl(url2, new LazyHeaders.Builder().addHeader("x-auth-token", token).build());
    FutureTarget<File> image2 = mGlide.load(glideUrl).downloadOnly(SIZE_ORIGINAL, SIZE_ORIGINAL);

    new ShareImagesTask(this, "image/png").execute(image1, image2);

Then created ShareImagesTask which allows you to share multiple images by completing the request to get the images files via Glide:

public class ShareImagesTask extends AsyncTask<FutureTarget<File>, Void, ArrayList<Uri>> {

    private final Context mContext;
    private final String mMimeType;

    public ShareImagesTask(Context context, String mimeType) {
        this.mContext = context;
        this.mMimeType = mimeType;

    protected ArrayList<Uri> doInBackground(FutureTarget<File>... targets) {
        final ArrayList<Uri> imageUris = new ArrayList<>(targets.length);

        for(FutureTarget<File> target : targets) {
            try {
                File file = target.get();
                Uri uri = FileProvider.getUriForFile(mContext, "com.myapp.fileprovider", file);
            } catch (Exception ex) {
                Log.w("SHARE", "Sharing failed for one or more image files.", ex);

        return imageUris;

    protected void onPostExecute(ArrayList<Uri> result) {
        if(!result.isEmpty()) {
            Intent intent = new Intent();
            intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, result);
            mContext.startActivity(Intent.createChooser(intent, "Share Your Photo"));

Here is my AndroidManifest.xml FileProvider:

        android:resource="@xml/filepaths" />

And finally, filepaths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="">
    <cache-path name="shared_images" path="/data/data/com.myapp/cache/image_manager_disk_cache/"/>

I am able to get to the point of calling FileProvider.getUriForProvider() and it gives the exception below. I have also tried changing the path in filepaths.xml to variations of the path mentioned in the exception but I get the same error. Any ideas?

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.myapp/cache/image_manager_disk_cache/11fa9845150748cc77d49a75954d92f246cc7095e72e36b85d6a07dbdeb9cf46.0



<cache-path name="shared_images" path="/data/data/com.myapp/cache/image_manager_disk_cache/"/>


<cache-path name="shared_images" />

or possibly with:

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

if you are trying to restrict FileProvider to a subset of getCacheDir().