Issue
I'm using Glide library to upload images. In another app, I use this code
void imageButtonclick() {
iv1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CropImage.activity(filePath).setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1,1).setOutputCompressQuality(50).start(UploadBook.this);
// Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//intent.putExtra(MediaStore.EXTRA_OUTPUT,imageuri);
//startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
});
to decrease the size of the image by declaring .setOutputCompressQuality(50)
But I don't know how to do the same with the Glide Image library.
My code is
public static void loadLocalImage(Context ctx, RequestOptions glideRequests, Uri uri, ImageView imageView,
RequestListener listener) {
glideRequests.fitCenter()
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE);
Glide.with(ctx)
.applyDefaultRequestOptions(glideRequests.placeholder(R.drawable.ic_stub).error(R.drawable.ic_stub))
.asBitmap()
.load(uri)
.listener(listener)
.into(imageView);
}
Solution
From Glide official site:
By default Glide prefers RGB_565 because it requires only two bytes per pixel (16 bit) and therefore has half the memory footprint of the higher quality and system default ARGB_8888.
So you cannot reduce the quality of a bitmap anymore, but you have some another options.
Option 1: Resizing image size with override(x, y)
.
RequestOptions myOptions = new RequestOptions()
.override(100, 100);
Glide.with(fragment)
.asBitmap()
.apply(myOptions)
.load(url)
.into(bitmapView);
Option 2: Scaling image with centerCrop
or fitCenter
centerCrop() is a cropping technique that scales the image so that it fills the requested bounds of the ImageView and then crops the extra. The ImageView will be filled completely, but the entire image might not be displayed.
RequestOptions myOptions = new RequestOptions()
.centerCrop();
Glide.with(ctx)
.asBitmap()
.apply(myOptions)
.load(url)
.into(bitmapView);
fitCenter() is a cropping technique that scales the image so that both dimensions are equal to or less than the requested bounds of the ImageView. The image will be displayed completely, but might not fill the entire ImageView.
RequestOptions myOptions = new RequestOptions()
.fitCenter();
Glide.with(ctx)
.asBitmap()
.apply(myOptions)
.load(url)
.into(bitmapView);
Option 3: Mix both of these solutions
RequestOptions myOptions = new RequestOptions()
.fitCenter() // or centerCrop
.override(100, 100);
Glide.with(ctx)
.asBitmap()
.apply(myOptions)
.load(url)
.into(bitmapView);
Answered By - Son Truong
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.