How many ways to convert bitmap to string and vice-versa?
Asked Answered
D

3

42

In my application i want to send bitmap image to the server in the form of string, i want to know how many ways are available to convert a bitmap to string. now i am using Base64 format for encoding and decoding, it takes little bit more memory. is there any other possibilities to do the same thing in different ways which takes less memory cosumptions. Now i am using this code.

Resources r = ShowFullImage.this.getResources();
Bitmap bm = BitmapFactory.decodeResource(r, R.drawable.col);
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
bm.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object   
byte[] b = baos.toByteArray();

String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
Disrespect answered 26/11, 2012 at 10:0 Comment(0)
L
92
public String BitMapToString(Bitmap bitmap){
     ByteArrayOutputStream baos=new  ByteArrayOutputStream();
     bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
     byte [] b=baos.toByteArray();
     String temp=Base64.encodeToString(b, Base64.DEFAULT);
     return temp;
}

Here is the reverse procedure for converting string to bitmap but string should Base64 encoding

/**
 * @param encodedString
 * @return bitmap (from given string)
 */
public Bitmap StringToBitMap(String encodedString){
   try {
      byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
      Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
      return bitmap;
   } catch(Exception e) {
      e.getMessage();
      return null;
   }
}
Liar answered 26/11, 2012 at 10:46 Comment(4)
already i am using base64 format, i am looking for different one.Disrespect
what is the difference between my code and your code, i am looking for different ways to convert other than using BAse64.Disrespect
StringToBitMap is coming as null. Help me please.Serviceable
Attempt to invoke virtual method 'byte[] java.lang.String.getBytes()' on a null object reference. That's what I got from StringToBitmapPyuria
I
4

Yes, You can do it by implenment this code :

String to Bitmap :

 public Bitmap StringToBitMap(String encodedString) {
    try {
        byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0,
                encodeByte.length);
        return bitmap;
    } catch (Exception e) {
        e.getMessage();
        return null;
    }
}

Bitmap to String :

public String BitMapToString(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] b = baos.toByteArray();
    String temp = Base64.encodeToString(b, Base64.DEFAULT);
    return temp;
}
Indigo answered 5/8, 2013 at 7:2 Comment(0)
D
3

you can use byteArray to send images or other data. there is no encoding and decoding require. and you have to use multipart body to send data to server..

Dinnage answered 26/11, 2012 at 10:4 Comment(1)
It would be really appreciable and useful if you could share with your answer how to do the image upload with multipart body? (may be with android and server code blocks)Acknowledge

© 2022 - 2024 — McMap. All rights reserved.