0

I have a .net ASMX webservice that I'm consuming using the ksoap2 library. In the service, I first save the user image and later retrieve it. However, once I retrieve it, the byte array is intact, but the BitmapFactory is unable to decode it and returns a null.

To convert to byte array:

Bitmap viewBitmap = Bitmap.createBitmap(imageView.getWidth(),
imageView.getHeight(), Bitmap.Config.ARGB_8888);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
viewBitmap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos);
byte[] bitmapdata = bos.toByteArray();

The webservice accepts the bytearray in the byte[] format.

To convert the array into bitmap:

byte[] blob= info.get(Main.KEY_THUMB_BYTES).getBytes();
Bitmap bmp=BitmapFactory.decodeByteArray(blob,0,blob.length); // Return null :(
imageView.setImageBitmap(bmp);

From partial-analysis, it appears that the byte array does not change. Then why does decoding return null? Is there a better to save an image and pass it through a webservice? I didn't analyze the whole byte array, so I'm guessing it might've changed a bit.

Any thoughts? Many thanks!

UPDATE: I just tried converting the byte[] to string using:

Base64.encodeToString( bos.toByteArray(), Base64.DEFAULT);

And decode using:

byte[] blob= Base64.decode(info.get(Main.KEY_THUMB_BYTES));

Now all i get is a White picture. I'm not sure what's wrong here. Please help.

UPDATE: I'm storing this image inside of a database, in a column of type varchar(max). Should I be storing this byte array string inside a different sql data type? I'm not too experienced with SQL, so I used varchar because it did not convert text to unicode, which I thought might be good for thie byte array.

Thanks!

Goo
  • 1,318
  • 1
  • 13
  • 31
harsimranb
  • 2,231
  • 1
  • 35
  • 55

1 Answers1

0

convert your byte arrays to Base64 that is a string and easy to transfer:

public static String bitmapToBase64(Bitmap bitmap) {
        byte[] bitmapdata = bitmapToByteArray(bitmap);
        return Base64.encodeBytes(bitmapdata);
    }

    public static byte[] bitmapToByteArray(Bitmap bitmap) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos);
        byte[] bitmapdata = bos.toByteArray();
        return bitmapdata;
    }

and

public static Bitmap base64ToBitmap(String strBase64) throws IOException {
    byte[] bitmapdata = Base64.decode(strBase64);
    Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0,
            bitmapdata.length);
    return bitmap;
}

also you can do it not only on image files but also on every file types:

public static String fileToBase64(String path) throws IOException {
    byte[] bytes = fileToByteArray(path);
    return Base64.encodeBytes(bytes);
}

public static byte[] fileToByteArray(String path) throws IOException {
    File imagefile = new File(path);
    byte[] data = new byte[(int) imagefile.length()];
    FileInputStream fis = new FileInputStream(imagefile);
    fis.read(data);
    fis.close();
    return data;
}


public static void base64ToFile(String path, String strBase64)
        throws IOException {
    byte[] bytes = Base64.decode(strBase64);
    byteArrayTofile(path, bytes);
}

public static void byteArrayTofile(String path, byte[] bytes)
        throws IOException {
    File imagefile = new File(path);
    File dir = new File(imagefile.getParent());
    if (!dir.exists()) {
        dir.mkdirs();
    }
    FileOutputStream fos = new FileOutputStream(imagefile);
    fos.write(bytes);
    fos.close();
}
Bob
  • 22,810
  • 38
  • 143
  • 225
  • I tried what you told me, and I'm still getting a white image. I wonder if this has anything to do with the webservice or the database. I'm storing the image as varchar(max), should I be using a different type? – harsimranb Jul 01 '12 at 05:22
  • Is the value of base64 that is sent to Webservice same as the value of base64 which is received from it? – Bob Jul 01 '12 at 06:00
  • well, it seems....It looks about the same, but I haven't compared it precisely because it is quite big....I can confirm this in a few minutes....... – harsimranb Jul 01 '12 at 19:31
  • @Pathachiever11 I have got the same problem...see this question http://stackoverflow.com/questions/14681643/image-isnt-creating-using-the-bitmapfactory-decodebytearray – Pragnani Feb 04 '13 at 11:20