Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 35
Registered: ‎01-10-2010
My Device: 8310 and 9000
My Carrier: no idea

Loading Bitmaps

[ Edited ]

Hi,

 

I have my image data available as a String value(as a long text value) and want the information to be converted to a .png or .jpg image

How do I do that in Blackberry as the usual method Bitmap.getBitmapResource(String module) works with a named resource provided as a String ?

 

Kindly help

 

Thanks in advance

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Loading Bitmaps

I'm sure there are other ways, but you can convert the String to a byte array and use EncodedImage,createEncodedImage(....) to create an encoded image.  From there you can get a Bitmap that you can display.

 

Search the forum for other posts that talk about EncodedImage for more details.

New Developer
Posts: 35
Registered: ‎01-10-2010
My Device: 8310 and 9000
My Carrier: no idea

Re: Loading Bitmaps

[ Edited ]

I tried working on similar grounds and used createEncodedImage as follows :

public static Bitmap loadBitmap(byte[] res) {
              EncodedImage img = null;
              System.out.println("Inside loadBitmap 1 with res as : " res.length);
              try{
                  img = EncodedImage.createEncodedImage(res,0,res.length);
              }
              catch(Exception e) {System.out.println("Exception message is " + 

              e.getMessage);System.out.println("Inside loadBitmap Exception caught");}
              return img.getBitmap();
        }         

The  'res' byte[] prints the size correctly.However, the createEncodedImage fails.

From the forum posts, til now I could find only this usage of createEncodedImage.

 

Any suggestions on this ?

 

 

 

 

 

 

Developer
Posts: 101
Registered: ‎07-20-2009
My Device: Not Specified

Re: Loading Bitmaps

If you are creating byte[] which is containing image data by using String.getBytes() method, then this might cause some image data loss.

 

I suggest you to use Base64InputStream class to convert string data to byte[].

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Loading Bitmaps

To use Base64 you will have to send the data base 64 encoded as well.

 

Just to expand on kara127' point, this suggests that the data your are supplying to EncodedImage is not actually correct and complete.  You need to check that it is exactly what you expect it to be.

 

Remember that if you convert it to a String on the Server, you must use exactly the same conversion on the Blackberry to convert it back to bytes.  The usual conversion on the Server will be to UTF-8.  However the default conversion on the BlackBerry is an ISO number I can't remember.  You can tell the BlackBerry to use UTF-8 by specifying this in the getBytes(), for example getBytes("UTF-8").

New Developer
Posts: 35
Registered: ‎01-10-2010
My Device: 8310 and 9000
My Carrier: no idea

Re: Loading Bitmaps

[ Edited ]

Thanks for providing with your suggestions.

 

However the above choices did not work.

 

Checking the code for the desktop application I found the following chunk which can convert the string image data to Object and all this is defined in a class Base64.java.

 

I tried importing the similar code for Blackberry but get an error for BufferedInputStream and ObjectInputStream as they are not part of the API 4.6.

 

Following is the code fragment which takes the String Image data as input :

 

public static Object stringToObject(String s) {
       
        byte bytes[] = decode(s);

        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        ObjectInputStream is = new ObjectInputStream( new BufferedInputStream(bais));  //ERROR

        return is.readObject();
        }

 

 

How do we do the required code replacements in BlackBerry ?

 

I even tried passing the constructed 'bytes' array (in the code above) directly to the createEncodedImage method and then used it  to create a BitMap, but that too

left me with the same result as an IllegalArgumentException.

 

Any further suggestions on this?

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Loading Bitmaps

Can we go back to this statement in your original post:

"I have my image data available as a String value"

 

How did you get it in this format?  In detail, can you explain the steps that were involved. 

 

Understanding how you create this String will help us understand how to interpret it. 

New Developer
Posts: 35
Registered: ‎01-10-2010
My Device: 8310 and 9000
My Carrier: no idea

Re: Loading Bitmaps

Hi,

 

There is a  method Base64.objectToString(textObj) where textObj represents a byte[] and the following is the implementation for the call to that method . This method finally returns the String which represents the image data stored in the DB.

 

public static String objectToString(Object o) {

ByteArrayMaker bam = new ByteArrayMaker(32000);

        try {
            ObjectOutputStream os = new ObjectOutputStream(
                new BufferedOutputStream(bam));

            os.flush();
            os.writeObject(o);
            os.flush();
        }
        catch (IOException e) {
            _log.error(e.getMessage());
        }

        return encode(bam.toByteArray()); 

        //this returned string gets stored in the database finally representing th image text

}

 

public static String encode(byte raw[]) {
        StringBuilder encoded = new StringBuilder();

        for (int i = 0; i < raw.length; i += 3) {
            encoded.append(encodeBlock(raw, i));
        }

        return encoded.toString();
    }

    protected static char[] encodeBlock(byte raw[], int offset) {
        int block = 0;
        int slack = raw.length - offset - 1;
        int end = slack < 2 ? slack : 2;

        for (int i = 0; i <= end; i++) {
            byte b = raw[offset + i];

            int neuter = b >= 0 ? ((int) (b)) : b + 256;
            block += neuter << 8 * (2 - i);
        }

        char base64[] = new char[4];

        for (int i = 0; i < 4; i++) {
            int sixbit = block >>> 6 * (3 - i) & 0x3f;
            base64[i] = getChar(sixbit);
        }

        if (slack < 1) {
            base64[2] = '=';
        }

        if (slack < 2) {
            base64[3] = '=';
        }

        return base64;
    }

 

 

Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Loading Bitmaps

And this code is implemented on a Server somewhere?

 

As there is not a stream to Object option on the Blackberry, I think you are going to have to convert your image into another form to transport it.  Or find out exactly what ObjectOutputStream will do with your Object. 

 

What sort of Object is it anyway?

 

Most people just read the jpg or png file and send the bytes from there.  That is by far the easiest way.