0

I am developing a function that downloads.JPG image from the Internet.

When I download using BitmapFactory.decodeStream() and save it in my internal storage, the file size is bigger than the original file.

The original image file(.JPG) size is 114KB and the size of the file what I saved is 261KB

b = BitmapFactory.decodeStream(urlConnection.getInputStream()); //Download original image

FileOutputStream fos = openFileOutput("x.jpg", MODE_PRIVATE);

b.compress(Bitmap.CompressFormat.JPEG, 100, fos);

File ne = new File(getFilesDir()+"/x.jpg");

String l = ""+ Formatter.formatFileSize(MainActivity.this,ne.length())+"byte"//261KB

edit:

InputStream is = urlConnection.getInputStream();

OutputStream outStream = MainActivity.this.openFileOutput("x.jpg", MODE_PRIVATE);

byte[] buf = new byte[1024]; int len = 0;
int total_len=0;

while ((len = is.read(buf)) > 0) {

outStream.write(buf, 0, len);
total_len+=len;

} 
is.close(); outStream.close();

//total_len = 116797 byte
//saved image file size = 116797 byte

Bitmap z = BitmapFactory.decodeFile(getFilesDir()+"/x.jpg");

img.setImageBitmap(z);
taejun
  • 23
  • 6
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/221666/discussion-on-question-by-taejun-why-downloaded-image-size-is-bigger-than-origin). – Samuel Liew Sep 18 '20 at 01:16

2 Answers2

0

You are putting it at 100 quality, which without compression. the original image is compressed by quality less than 100, but when you decode the stream, it converts it to an uncompressed bitmap. and when you save it at 100 quality, it will for sure be much greater than the original.

so try something like b.compress(Bitmap.CompressFormat.JPEG, 70, fos);... to a level that doesn't make it look bad. and it will be as you want.

edit: you may download the image file directly unchanged if you want it intact, look at How to download file/image from url to your android app

Mereb Hayl
  • 58
  • 7
  • thanks for help. but what should i do if i want to download and save image about original quality and size? – taejun Sep 16 '20 at 13:59
  • in that case you may need to know the compression quality the original was compressed with. But you definitely can not, since there is no ubiquitous definition of the JPEG compression level. The actual result when saving a JPEG with compression level 60 in one software can differ significantly from what another software produces when set to level 60. so I would advice you to set a standard of your own, with a suitable quality number and use standard for your purpose. – Mereb Hayl Sep 16 '20 at 15:43
  • or it would be better to download the `Image file` directly without converting it to bitmap... you may see https://stackoverflow.com/questions/9762057/how-to-download-file-image-from-url-to-your-android-app – Mereb Hayl Sep 16 '20 at 15:56
  • Thanks, I did what you said. now Original = 114kb and saved file = 116kb. then 2kb is just dummy value? – taejun Sep 16 '20 at 18:09
  • good that it worked as you wanted. if my answer was helpful, mark it as the answer and upvote it. Thanks! – Mereb Hayl Sep 30 '20 at 08:47
0

You should not first convert your jpg file to a bitmap.

Do away with Bitmap and BitmapFactory.

Save, in a loop, the readed bytes directly to a FileOutputStream for the destination file.

Then you write as many bytes, and the same, to file as downloaded.

Having done that your code is suitable to download any file type.

blackapps
  • 8,011
  • 2
  • 11
  • 25
  • Is that essential to save 'image files' from Bitmap.compress() ? and How to read the image from the file? – taejun Sep 16 '20 at 13:55
  • Isn't it essential to use BitmapFactory.compress() to store images? If I do as you say, how can I read the image file and use it as a bitmap? – taejun Sep 16 '20 at 14:09
  • That is a completely different problem. You were complaining that the saved jpg was different in size from original. I gave you a solution for that. If you later wanna use that file to make a bitmap then do it later with decodeFile(). – blackapps Sep 16 '20 at 14:12
  • i posted it.. iam very sorry for wasting your time – taejun Sep 16 '20 at 14:22
  • I did it..!! now original size is 114KB, and saved image size is 117KB. what is 3KB?? just dummy? – taejun Sep 16 '20 at 15:00
  • That is impossible. Your code is ok but outStream.close(); is missing. Tell both sizes in bytes. Not in KB. Every byte counts. – blackapps Sep 16 '20 at 15:28
  • Original = 114070, Saved = 116767. Source code is works. but about 2kb is bigger. – taejun Sep 16 '20 at 18:06
  • Unbelievable. And is the file corrupt or does it display well? And how did you check the sizes? – blackapps Sep 16 '20 at 18:10
  • no it is good quality. i checked from "File f = new File(~/x.jpg); f.length();" Is the difference in file size of 3kb a problem? – taejun Sep 16 '20 at 19:07
  • yes i added after 'ous.write()' like outStream.close(); – taejun Sep 17 '20 at 11:27