Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 853
Stefan Avatar asked Jul 21 '17 18:07

Stefan


People also ask

How do you refresh Glide?

You click sync in Glide If you are building your project in Glide and need to see the latest data from your data source, you can click the sync button.

How do you download pictures from Glide?

To simply load an image to LinearLayout, we call the with() method of Glide class and pass the context, then we call the load() method, which contains the URL of the image to be downloaded and finally we call the into() method to display the downloaded image on our ImageView.


3 Answers

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;
            }
like image 135
Zeeshan Shabbir Avatar answered Oct 16 '22 14:10

Zeeshan Shabbir


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.

like image 36
Ajeet Choudhary Avatar answered Oct 16 '22 16:10

Ajeet Choudhary


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.

like image 20
Reyansh Mishra Avatar answered Oct 16 '22 14:10

Reyansh Mishra