0

I have a problem in loading image using glide. I am trying to load an image after picking image from gallery, when I tried to load an image using Android Default setImageBitmap(bitmap); It's worked but I faced memory leak issue.

Then I tried Glide image loader to avoid memory leak issue. But I can't load image using File Path.

 Glide.with(_A) .load(new File(uri.getPath())) .asBitmap() .override(w, w) .centerCrop() .placeholder(R.drawable.diploma) .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView); 

I also tried to load Uri but that too doesn't worked.

Then I tried to load image from URL Glide loaded URL image. That worked perfectly.

 Glide .with(_A) .load("https://yt3.ggpht.com/-v0soe-ievYE/AAAAAAAAAAI/AAAAAAAAAAA/OixOH_h84Po/s900-c-k-no-mo-rj-c0xffffff/photo.jpg") .asBitmap() .override(w, w) .centerCrop() .placeholder(R.drawable.diploma) .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView); 

Then I tried Default function to load image from Uri imageView.setImageURI(uri); This function worked Image loaded.

Uri doesn't have an Issue, Then why I can't load an image?

Can anyone help me to find the solution to fix this issue ?

6
  • Your code looks fine. Check if you are getting the right path. The code use are using inside .'load', try it in Log and check if the path is correct. Commented Apr 9, 2017 at 18:33
  • I have checked Uri It is a valuable path, Also I tried to to load Uri directly using this function imageView.setImageURI(uri); Image loaded. Commented Apr 10, 2017 at 5:30
  • Showing something in Logcat? Also use this code to get path 'file.getAbsolutePath()' Commented Apr 10, 2017 at 6:17
  • "Source code does not match the bytecode" This is what I got when I used file in Glide. I tried this answer stackoverflow.com/a/40566459/3615605 but issue not cleared Commented Apr 10, 2017 at 7:14
  • Should I post an answer on how I save image at specific path and then retrieve the image? Commented Apr 10, 2017 at 17:14

1 Answer 1

1

As discussed, here is the code to save the image at a particular path and also how to retrieve it. Also, I'm using Realm to save the path of the image, you can use anything else. Here is the code to save image:

private void saveToInternalStorage(Bitmap bitmapImage, final String fileName) { ContextWrapper cw = new ContextWrapper(getContext()); directory = cw.getDir("myloImages", Context.MODE_PRIVATE); if (!directory.exists()) { directory.mkdir(); } mypath = new File(directory, fileName + ".png"); FileOutputStream fos = null; try { fos = new FileOutputStream(mypath); bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.close(); Log.v("IMAGE PATH", mypath.getAbsolutePath()); } catch (Exception e) { Log.e("SAVE_IMAGE", e.getMessage(), e); } realm.executeTransactionAsync(new Realm.Transaction() { @Override public void execute(Realm realm) { PhotosRealm photosRealm = new PhotosRealm(); photosRealm.setImageLocation(directory.getAbsolutePath() + "/" + fileName + ".png"); realm.copyToRealm(photosRealm); // I'm using realm to save location. } }); finish(); } 

And now to retrieve the image from that location. It is same as your code:

private RealmResults<PhotosRealm> list; Glide.with(context).load(list.get(0).getImageLocation()).asBitmap().into(imageView); 
Sign up to request clarification or add additional context in comments.

1 Comment

I will check it tonight and let you know, Thanks a lot :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.