PDA

View Full Version : PNG/JPG file to byte array and vice-verca



svishwa11
07-31-2009, 12:25 PM
Hi,
I am trying to convert image file to byte array and vice-verca.
Reading a image file (PNG/JPG) from SD Card and converting to byte array. This is at the sender side. At the reciever, the incoming byte array needs to be converted back to image/bitmap and written to the SD card. I have been trying different ways of doing this but nothing seems to work. Here is the code.

Trial 1. In this case, decodeByteArray returns null.

Sender: Image File to Byte Array
String filepath = "/sdcard/";
File imagefile = new File(filepath + "icon.png");
FileInputStream fis = new FileInputStream(imagefile);
Bitmap bi = BitmapFactory.decodeStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] data = baos.toByteArray();

Receiver: Byte Array to Image
Bitmap bitmapimage = BitmapFactory.decodeByteArray(incomingbytearray, 0, incomingbytearray.length);
String filepath = "/sdcard/tlogo.png";
File imagefile = new File(filepath);
FileOutputStream fos = new FileOutputStream(imagefile);
bitmapimage.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();

I dont understand why decodebytearray returns null.

Trial 2: Normal fileoutputstream avoiding bitmap totally.

Sender
String filepath = "/sdcard/";
File imagefile = new File(filepath + "icon.png");
byte[] data = new byte[(int) imagefile.length()];
FileInputStream fis = new FileInputStream(imagefile);
fis.read(data);
fis.close();

Receiver
String filepath = "/sdcard/tlogo.png";
File imagefile = new File(filepath);
FileOutputStream fos = new FileOutputStream(imagefile);
fos.write(incomingbytearray);

This writes the bytes but not able to view/open the written file. Looks like the file is corrupted. I guess this is not the way for image files.

What is the proper way of doing this?

Thanks,
Vishwa

Aselby
12-02-2010, 09:50 AM
Did you ever find the problem ?

I've been running in to this same kind of issue, I thought that my problem was in the network connection, but I've tried different things but sometimes my image gets corrupt. I'd be really interested to see if you found a problem and how to solve it.

Thanks

nico
12-02-2010, 12:11 PM
Hi,
Trial 1. In this case, decodeByteArray returns null.

Sender: Image File to Byte Array
String filepath = "/sdcard/";
File imagefile = new File(filepath + "icon.png");
FileInputStream fis = new FileInputStream(imagefile);
Bitmap bi = BitmapFactory.decodeStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] data = baos.toByteArray();
...
I dont understand why decodebytearray returns null.

Trial 2: Normal fileoutputstream avoiding bitmap totally.
..

This writes the bytes but not able to view/open the written file. Looks like the file is corrupted. I guess this is not the way for image files.


Your code above seems correct to me. You probably mix up the bytearray before, since it does not work as expected even when omitting Bitmap decoding / encoding



What is the proper way of doing this?


What is going on between your sender and your receiver? Do you use Java Object serialization to pass that thing (this is incompatible afaik).

if you place a regular .png file to your sdcard and simply use the code in trial 1 of the sender side, everything should work. does it?
please also review whether you properly close and flush your buffers, this is important e.g. if you use a GZipOutputStream in between otherwise the byte buffers do not submit the complete data.

regards, nico