Android Forums

Go Back   TalkAndroid Forums > Android Coders > Android Development, Answers, Tutorials, and Code Snippets
Connect with Facebook

Click Here To Register!
 
 
LinkBack Thread Tools Display Modes
Old 07-30-2009, 07:23 PM   #1
Junior Member
 
Join Date: Jul 2009
Posts: 1
Friends: 0
View vishwa's Profile   View vishwa's Photo Album   View vishwa's Blog   View Social Groups
Hi,
I am trying to convert image file to byte array and vice-verca.

Basically, it involves two parts. 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 tried several variations and nothing seems to work. Here is the code for 2 such trials.

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();

Trial 2: Normal fileoutputstream avoiding bitmap totally.

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.

I dont understand why decodebytearray fails in the first method. Am I missing something??

Thanks
Vishwa
vishwa is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
 

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Zooming effects for image swarup Android Development, Answers, Tutorials, and Code Snippets 0 07-30-2009 04:07 AM
How do you zoom an image? droidan Android Development, Answers, Tutorials, and Code Snippets 1 04-16-2009 04:51 AM
This is how I open an image from an ImageButton droidan Android Development, Answers, Tutorials, and Code Snippets 0 04-05-2009 09:22 AM
Image upload direct from G1 jabtas Android Chat 0 12-20-2008 03:22 AM


Unlocked G1 Phones | Buy T-Mobile G1 | Google Phone

All times are GMT -6. The time now is 12:44 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.
Copyright (c) 2012 TalkAndroid.com. All rights reserved.