Android Community
Results 1 to 3 of 3

Thread: PNG/JPG file to byte array and vice-verca

  1. #1
    Join Date
    Jul 2009
    Posts
    1

    Default PNG/JPG file to byte array and vice-verca


    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

  2. #2
    Join Date
    Dec 2010
    Posts
    3

    Default Re: PNG/JPG file to byte array and vice-verca

    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

  3. #3
    Join Date
    Dec 2010
    Location
    Germany
    Posts
    7

    Default Re: PNG/JPG file to byte array and vice-verca


    Quote Originally Posted by svishwa11 View Post
    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
    Last edited by nico; 12-02-2010 at 11:14 AM.
    Developer of AcePic - use Facebook social network photos in your phone contacts without synchronizing
    http://www.weebmeister.com/acepic

Similar Threads

  1. How to build .apk file
    By jagtap.jj1 in forum All About Andorid Software
    Replies: 1
    Last Post: 06-17-2011, 04:19 PM
  2. How to open a file in the file explorer application
    By asifk1234 in forum General Phone Chat
    Replies: 7
    Last Post: 05-06-2010, 07:31 AM
  3. file transfer
    By vertigo in forum General HTC Chat
    Replies: 1
    Last Post: 06-02-2009, 12:27 AM
  4. file sending?!
    By Vinter in forum All About Andorid Software
    Replies: 8
    Last Post: 02-06-2009, 06:21 PM
  5. Need help moving file
    By barger01 in forum Developers Guild
    Replies: 4
    Last Post: 01-30-2009, 09:31 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •