0

I'm using Glide 4.7.1 trying to resize and save some photos.

for (String path : this.paths) {
        Glide.with(this.context).asBitmap().load(path).apply(RequestOptions.fitCenterTransform()).into(new SimpleTarget<Bitmap>(1080, 1920) {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                try {
                    resource.compress(Bitmap.CompressFormat.JPEG, 85, new FileOutputStream(MasterFacade.getAppDir() + "temp/" + resource.hashCode() + ".jpg"));
                    listener.onUpdate(progressMessage, processed.get());

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
    }

I just want to resize (keeping aspect ratio) to 1080x1920 (or the nearest) and then save the new bitmap to a file.

I'm sure I've permission to write I'm sure the path image exists I'm sure MasterFacade.getAppDir() + "temp/" + resource.hashCode() + ".jpg" is a valid dir and I've writing permisions on it

But this code is never reached!

I tried to debug and onResourceReady never is called...

What am I doing wrong?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rafael Lima
  • 3,079
  • 3
  • 41
  • 105

6 Answers6

2

Resize the image using this in Glide 4.x

Glide.with(getContext())
.load(imagePath)
.apply(new RequestOptions().override(600, 200))
.fitCenter() 
.into(setImage);
Gautam Kumar
  • 351
  • 4
  • 7
1

I believe none of the answer really read my problem:

I tried to debug and onResourceReady never is called...

So I checked glide wiki and it says:

The second critical part is the Glide builder line .with(context). The issue here is actually a feature of Glide: when you pass a context, for example the current app activity, Glide will automatically stop the request when the requesting activity is stopped

I was calling this code above right after call finish() on the activity so the code was internally stopped by Glide without throwing any exception and onResourceReady was never called.

Rafael Lima
  • 3,079
  • 3
  • 41
  • 105
1

get the image as @Gautam Kumar has done but you also have to change your imageView in the XML by adding android:adjustViewBounds="true".

Glide.with(getContext()) .load(imagePath) .apply(new RequestOptions().override(600, 200)) .into(setImage);

then your XML

      <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:id="@+id/image_id"
        android:src="@drawable/ic_image" />
wanjiku
  • 251
  • 6
  • 14
0

You can resize image with glide using override function.

You can get more from https://stackoverflow.com/a/50617561/6238866

Like :

public static void loadCircularImageGlide(String imagePath, ImageView view) {
    Glide.with(view.getContext())
            .load(imagePath)
            .asBitmap()
            .override(1080,1920) // resizes the image to these dimensions (in pixel). resize does not respect aspect ratio
            .error(R.drawable.create_timeline_placeholder)
            .fitCenter() // scaling options
            .transform(new CircularTransformation(view.getContext())) // Even you can Give image tranformation too
            .into(view);
}
Mayur Patel
  • 2,300
  • 11
  • 30
  • doens't work for glide 4.7.1... `override(x,y)` doens't even exist anymore – Rafael Lima Jun 12 '18 at 05:35
  • @RafaelLima You have to check this documents there for existence of override methord in 4.7.1. https://github.com/codepath/android_guides/wiki/Displaying-Images-with-the-Glide-Library – Mayur Patel Jun 12 '18 at 05:39
0

The problems seems to be causing by path , In Glide if you want to load image from external memory , You have to use correct path

Make Sure the path should be valid

String path="file://"+"image path";

for (String path : this.paths) {
        Glide.with(this.context).asBitmap().load("file://"+path).apply(RequestOptions.fitCenterTransform()).into(new SimpleTarget<Bitmap>(1080, 1920) {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                try {
                    resource.compress(Bitmap.CompressFormat.JPEG, 85, new FileOutputStream(MasterFacade.getAppDir() + "temp/" + resource.hashCode() + ".jpg"));
                    listener.onUpdate(progressMessage, processed.get());

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
    }
Quick learner
  • 10,632
  • 4
  • 45
  • 55
  • Would you tell how to do this for gifs. Because it does not changes the size of gif but the static images like png jpg – CrackerKSR Mar 20 '22 at 09:41
0

You can resize image with glide.try it...

Glide.with(getContext()) .load(imagePath) .apply(new RequestOptions().override(600, 200)) .fitCenter() .into(setImage);

Monali
  • 173
  • 1
  • 6