2

I have a web service, which gives me a json having a node named as 'imagedata'. It contains a huge data as a string. When I print this in browser it gives me valid input. Base64 encoded strings ends on '=' character.

I have also tested it using this tag in a html page, and it works perfectly fine.

<img src="data:image/png;base64,MY_BASE64_ENCODED_STRING"/>

Here is my code;

StringBuilder b64 = new StringBuilder(dataObj.getString("imagedata"));
byte[] decodedByte = Base64.decode(b64.toString(), 0);
bitmap = BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);

Kindly note that, This code works on smaller image-data but gives bad-base64 exception on larger image-data

Kindly help me out, Thanks

umirza47
  • 940
  • 2
  • 10
  • 21

1 Answers1

0

Why your server give you the base64 encoding?. Base64 it is just communication not to encoding image. If it use for encoding it will make your image file size bigger.IllegalArgumentException mean your image encoding incorrectly formatted or otherwise cannot be decoded. In my project i just, for now, i use the Base64 for sending image. But it will be change by multipart. But when server forward to the recipient. It just forward the url of image. So i can do simple process the url to Image with this:

public static Image loadImage(String url)
{
    HttpConnection connection = null;
    DataInputStream dis = null;
    byte[] data = null;

    try
    {
        connection = (HttpConnection) Connector.open(url);
        int length = (int) connection.getLength();
        data = new byte[length];
        dis = new DataInputStream(connection.openInputStream());
        dis.readFully(data);
    }
    catch (Exception e)
    {
        System.out.println("Error LoadImage: " + e.getMessage());
        e.printStackTrace();
    }
    finally
    {
        if (connection != null)
            try
            {
                connection.close();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        if (dis != null)
            try
            {
                dis.close();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }


    return Image.createImage(data, 0, data.length);
}

Note this code for J2ME.

iqbal
  • 35
  • 1
  • 4