Rafael Lima
Rafael Lima

Reputation: 3535

Resize and save images

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?

Upvotes: 0

Views: 4700

Answers (6)

wanjiku
wanjiku

Reputation: 269

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" />

Upvotes: 0

Gautam Kumar
Gautam Kumar

Reputation: 399

Resize the image using this in Glide 4.x

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

Upvotes: 1

Rafael Lima
Rafael Lima

Reputation: 3535

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.

Upvotes: 1

Monali
Monali

Reputation: 171

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

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

Upvotes: 0

Quick learner
Quick learner

Reputation: 11457

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();
                }
            }
        });
    }

Upvotes: 0

Mayur Patel
Mayur Patel

Reputation: 2326

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);
}

Upvotes: 0

Related Questions