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
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