2

I have a high resolution image Uri and I would like to reduce the image quality and size since i'm uploading it to a server and then load it afterwards as a smaller version of the original image.

I tried this:

Picasso.with(context).load(image).resize(100,100).get();

But it didn't work cause you need to do this from a separate Thread and I didn't find a listener that tells you when the resizing is over.

Does anyone has a solution that can reduce the image quality to get a smaller file size?

ben
  • 1,064
  • 3
  • 15
  • 29

2 Answers2

2
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 50;
Bitmap bmpSample = BitmapFactory.decodeFile(fileUri.getPath(), options);

ByteArrayOutputStream out = new ByteArrayOutputStream();                
bmpSample.compress(Bitmap.CompressFormat.JPEG, 1, out);
byte[] byteArray = out.toByteArray();
return convertToUri(byteArray);

Thank you Android: Compress Bitmap from Uri

Community
  • 1
  • 1
ben
  • 1,064
  • 3
  • 15
  • 29
1

Step #1: Call openInputStream() on a ContentResolver, passing in your Uri.

Step #2: Create a BitmapFactory.Options object, with an inSampleSize to control the degree of downsampling that you want to do, to create a lower-resolution Bitmap.

Step #3: Pass the stream and the BitmapFactory.Options object to decodeStream() on BitmapFactory, to get a downsampled Bitmap.

Step #4: Upload that Bitmap, where the details will vary a bit by whatever API you are using to do the upload (byte[], File, etc.).

Step #5: Do steps #1-4 on a background thread, as you should not be doing disk I/O, bitmap downsampling, and network operations on the main application thread.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491