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 ?

MathankumarK
  • 2,717
  • 1
  • 17
  • 34
  • 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. – Abhi Apr 09 '17 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. – MathankumarK Apr 10 '17 at 05:30
  • Showing something in Logcat? Also use this code to get path 'file.getAbsolutePath()' – Abhi Apr 10 '17 at 06:17
  • "Source code does not match the bytecode" This is what I got when I used file in Glide. I tried this answer http://stackoverflow.com/a/40566459/3615605 but issue not cleared – MathankumarK Apr 10 '17 at 07:14
  • Should I post an answer on how I save image at specific path and then retrieve the image? – Abhi Apr 10 '17 at 17:14
  • Yeah, Thank you :-) – MathankumarK Apr 12 '17 at 06:07

1 Answers1

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);
Abhi
  • 2,115
  • 2
  • 18
  • 29