0

I am creating an android app and want to read a file from sdcard and send it to a rest webservice. Everything works fine but when I read the file from sdcard its filesize is much bigger than its originalsize. A 10 KB file is getting 260 KB.

I am doing the following...

File f = new File(uri);
Log.d("ORIGINAL FILESIZE:"+f.length()); 

Filesize 10752 Bytes on SDCARD.

This is exactly the same size the image has on the phone.

Bitmap bmp = BitmapFactory.decodeFile(uri);
ByteArrayOutputStream bao = new ByteArrayOutputStream();

bmp.compress(Bitmap.CompressFormat.JPEG, 100, bao);
Log.d("FILESIZE AFTER:"+bao.size());

Filesize 260904 Bytes after decode / compress.

This is the filesize the server receives and writes to disk. It's the same Image and Quality but about 20 times bigger.

Does anyone know what I am doing wrong?

Chris
  • 566
  • 1
  • 8
  • 20

2 Answers2

0

If your original file is a jpeg with high compression rate, it's normal that the decoded bitmap is much larger, especially if you use a quality of 100 in the compress() method (which means high quality).

Did you try using compress() with a lower quality value (cf documentation) ?

sdabet
  • 18,360
  • 11
  • 89
  • 158
  • hi but doesn't the bmp.compress method just read the already compressed image from disk?. I don't want to compress it double times. I want to leave the quality as it is. – Chris Jul 11 '12 at 15:17
  • not sure. Maybe not. I just need to read the image from sdcard and need to get a byte[] array. Is there a better way doing it? – Chris Jul 11 '12 at 15:26
  • This may help: http://stackoverflow.com/questions/6058003/beautiful-way-to-read-file-into-byte-array-in-java – sdabet Jul 11 '12 at 15:30
0

It is not wrong. Or at least it could be. The bytes rapresentation of a bitmap in memory, requires (a 32-bit image) height * widht * 4 bytes. And of course it differs from the file size becaouse of the compression.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305