0

I used the below code for import gallery images to my ImageView but it reduces image size. If the original image size is above 5MB, after importing and saving the image, it reduces the image size to 2MB and the quality is lost. Any suggestion for getting an image from the gallery and saving without reducing the size and quality?

Bitmap bitmapImage = BitmapFactory.decodeFile(images.get(position));
setBitmap(bitmapImage);
Attiq ur Rehman
  • 475
  • 1
  • 6
  • 21
  • https://stackoverflow.com/questions/11061280/android-reduce-image-file-size – ManishNegi Jan 14 '20 at 10:15
  • How are you saving the image. Share the code – Attiq ur Rehman Jan 14 '20 at 10:31
  • `FileOutputStream out = new FileOutputStream(file, false);` ` saveBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);` `out.flush();` `out.close();` `MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());` – Isuru Gunasekara Jan 14 '20 at 10:43
  • i debug my code when import from gallery, bit map size 2100 , 1080. but checked my phone and original res size 4200, 2800 like that.. i think size reducing in importing time. am i right? – Isuru Gunasekara Jan 14 '20 at 10:47
  • You are talking about image size instead of file size. And then you mention resolutions. So you are resizing the original file somewhere. Please post reproducable complete code in your post. Not in comments. The comments . are for us. And we have nothing with `images.get(position)`. Better give the actual value. – blackapps Jan 14 '20 at 11:20
  • use glide it has few options how to download and how to display. check RequestOptions https://futurestud.io/tutorials/tag/glide – Kirguduck Jan 14 '20 at 11:22

1 Answers1

0

Change in size is happening because of the compressing format and the scales factors. There is a small problem in your approach.

Bitmap bitmapImage = BitmapFactory.decodeFile(images.get(position));
setBitmap(bitmapImage);

I will encourge you to check the size of bitmap, use method bitmap.getByteCount() . You can observer the difference in size, don't get condused. It was because of the compression. (PNG, JPG actually store the image in compressed.

You should use BitmapFactor.Options to load the same size as the bitmap.

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bitmapImage = BitmapFactory.decodeFile(images.get(position), options);

You can see that we configured our option by setting inJustDecodeBounds=true. It gives the information(width, height, etc.) about image.

Attiq ur Rehman
  • 475
  • 1
  • 6
  • 21