Stefan
Stefan

Reputation: 2188

Glide can't load file from internal storage

I have an Activity in which I download a zipfile of images and then unzip in the getFilesDir(). Path is like this :

/data/user/0/packagename/files/files/example.png

When I try to load these images however, they're not showing. I'm using this code to get the path and load the image:

   String loc = getFilesDir().getPath() + "/" + Config.IMAGES_LOCATION +"/";
   String imageloc = loc + model.getThumbnail();
   Glide.with(ActivityImageGallery.this).load(imageloc).into(image);

The imageloc path is the same as the save location and when creating a file from the path, it shows that it would exist.

I tried using file:// in front of the path, but that doesn't work either. WRITE_EXTERNAL en READ_EXTERNAL permissions are asked and granted.

Upvotes: 15

Views: 13718

Answers (5)

Zeeshan Shabbir
Zeeshan Shabbir

Reputation: 7114

This works for me, make file instance and pass file's uri that you want to load into ImageView

Glide.with(context)
    .load(new File(fileUri.getPath())) // Uri of the picture
    .into(profileAvatar);

Make sure you have added

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

You can also use RequestListener to trace the error in case of failure

new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                return false;
            }

Upvotes: 19

Nikhil
Nikhil

Reputation: 107

You would need run time permissions to use internal storage.Second use latest glide library compile 'com.github.bumptech.glide:glide:3.7.0' and format as

Glide.with(context).load(image path).into(Imageview);

Upvotes: 0

Ajeet Singh
Ajeet Singh

Reputation: 2009

First capture your image path,either you are capturing image from camera or picking it from gallery(I assume you have already given read and write permissions). Then Try this

Glide.with(context).load(new File(imagePath)).dontAnimate().error(R.drawable.errorImage).placeholder(R.drawable.placeholderImage).into(imageView);

Using .dontAnimate() because in some case it may be that image did not load or show when you load first time(or take extra time). Hope it will help.

Upvotes: 3

AskNilesh
AskNilesh

Reputation: 69734

try this

File c = new File(Environment.getExternalStorageDirectory() + "/data/user/0/packagename/files/files/example.png");
Glide.with(MainActivity.this).load(c).error(R.drawable.empty_pic).placeholder(R.drawable.empty_pic).into(image2);

or try this

 Glide.with(getActivity()).load(new File(" your path").toString())
        .listener(new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                Log.e("xmx1","Error "+e.toString());
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                Log.e("xmx1","no Error ");
                return false;
            }
        })
        .into(ivx);

Upvotes: 0

Reyansh Mishra
Reyansh Mishra

Reputation: 1915

Did you try using loading listener of glide using this

1.Debug:-

https://stackoverflow.com/a/42067675/5492047

Glide.with(getActivity())
 .load(args.getString(IMAGE_TO_SHOW))
 .listener(new RequestListener<String, GlideDrawable>() {
     @Override
     public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
         return false;
     }

     @Override
     public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {

         return false;
     }
 })
 .into(imageFrame);

and check the exception ?

2.Debug Again.

Can you use any file explorer app and go to the exact place where the file is loaded and open then file?.

3.Path

Is it really /files/files/ in the path?

4.Extension Can you remove .png from the file path and load and check?

5.Tutorial

An awesome tutorial can be found here in depth https://futurestud.io/tutorials/glide-getting-started.

Upvotes: 2

Related Questions