1

I receive a binary base64 image as a string via a json array in my application. I want to convert this string to a byte array and show it in my application as an image.

This is what I came up with:

byte[] data = Base64.decode(image, Base64.DEFAULT);
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
thumbNail.setImageBitmap(bmp);

The variable image is the base64 string that I receive from the json array.

1
  • 3
    And what is the actual problem? Does your code throw any Exceptions? You should emphasize more on the troubles you have. Commented Jul 25, 2015 at 17:15

2 Answers 2

3

Use this Apache Commons Codec in your project.

byte[] decoded = org.apache.commons.codec.binary.Base64.decodeBase64(imageString.getBytes()); 

Or use Sun's options:

import sun.misc.BASE64Decoder;

byte[] byte;
try 
{
    BASE64Decoder decoder = new BASE64Decoder();
    byte = decoder.decodeBuffer(imageString);
} 
catch (Exception e) 
{
    e.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

1 Comment

This method is deprecated.
1

String to GZIP and GZIP to String, full class

class GZIPCompression {
static String compress(String string) throws IOException {
    if ((string == null) || (string.length() == 0)) {
        return null;
    }
    ByteArrayOutputStream obj = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(obj);
    gzip.write(string.getBytes("UTF-8"));
    gzip.flush();
    gzip.close();
    return Base64.encodeToString(obj.toByteArray(), Base64.DEFAULT);
}

static String decompress(String compressed) throws IOException {
    final StringBuilder outStr = new StringBuilder();
    byte[] b = Base64.decode(compressed, Base64.DEFAULT);
    if ((b == null) || (b.length == 0)) {
        return "";
    }
    if (isCompressed(b)) {
        final GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(b));
        final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            outStr.append(line);
        }
    } else {
        return "";
    }
    return outStr.toString();
}

private static boolean isCompressed(final byte[] compressed) {
    return (compressed[0] == (byte) (GZIPInputStream.GZIP_MAGIC)) && (compressed[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8));
}}

for call

// test compress
    String strzip="";
    try {
        String str = "Main TEST";
        strzip=GZIPCompression.compress(str);
    }catch (IOException e){
        Log.e("test compress", e.getMessage());
    }
    //test decompress
    try {
        Log.e("src",strzip);
        strzip = GZIPCompression.decompress(strzip);
        Log.e("decompressed",strzip);
    }catch (IOException e){
        Log.e("test decompress", e.getMessage());
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.