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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Highlighted
Regular Contributor
Posts: 83
Registered: ‎05-24-2011
My Device: Storm 9550
My Carrier: XXX

getting an image from url - no stack trace error

Hi,

 

I am trying to get a Bitmap from a url and I am having trouble getting it. Here is what I am using:

 

package net.creative.core;

public interface WebDataCallback  
{  
    public void callback(String data);  
} 

 

package net.creative.core;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import net.rim.device.api.ui.UiApplication;

public class Utils {
	
	
    public static void getWebData(final String url, final WebDataCallback callback) throws IOException  
    {  
        Thread t = new Thread(new Runnable()  
        {  
            public void run()  
            {  
                HttpConnection connection = null;  
                InputStream inputStream = null;  
      
                try  
                {  
                    connection = (HttpConnection) Connector.open(url, Connector.READ_WRITE, true);  
                    inputStream = connection.openInputStream();  
                    byte[] responseData = new byte[10000];  
                    int length = 0;  
                    StringBuffer rawResponse = new StringBuffer();  
                    while (-1 != (length = inputStream.read(responseData)))  
                    {  
                        rawResponse.append(new String(responseData, 0, length));  
                    }  
                    int responseCode = connection.getResponseCode();  
                    if (responseCode != HttpConnection.HTTP_OK)  
                    {  
                        throw new IOException("HTTP response code: "  
                                + responseCode);  
                    }  
      
                    final String result = rawResponse.toString();  
                    UiApplication.getUiApplication().invokeLater(new Runnable()  
                    {  
                        public void run()  
                        {  
                            callback.callback(result);  
                        }  
                    });  
                }  
                catch (final Exception ex)  
                {  
                    UiApplication.getUiApplication().invokeLater(new Runnable()  
                    {  
                        public void run()  
                        {  
                            callback.callback("Exception (" + ex.getClass() + "): " + ex.getMessage());  
                        }  
                    });  
                }  
                finally  
                {  
                    try  
                    {  
                        inputStream.close();  
                        inputStream = null;  
                        connection.close();  
                        connection = null;  
                    }  
                    catch(Exception e){}  
                }  
            }  
        });  
        t.start();  
    }  
}

 

 

package net.creative.core;

import net.rim.device.api.math.Fixed32;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.component.BitmapField;

public class WebBitmapField extends BitmapField implements WebDataCallback  
{  
    public EncodedImage image = null;  
    private Bitmap bitmap = null;
  
    public WebBitmapField(String url)  
    {  
        try  
        {  
            Utils.getWebData(url, this);  
        }  
        catch (Exception e) {}  
    }  
  
    public Bitmap getBitmap()  
    {  
    	bitmap = image.getBitmap();
        if (bitmap == null) return null;  
        return image.getBitmap();  
    }  
  
    public void callback(final String data)  
    {  
        if (data.startsWith("Exception")) return;  
  
        try  
        {  
            byte[] dataArray = data.getBytes();  
            image = EncodedImage.createEncodedImage(dataArray, 0, dataArray.length);
            bitmap = image.getBitmap();
                      	
            
        }  
        catch (final Exception e){}  
    }  

} 

 

 

I keep getting a no stack trace error whenever I try to access the image.

BlackBerry Development Advisor
Posts: 15,885
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: getting an image from url - no stack trace error

You should use a ByteBuffer instead of a StringBuffer to store the binary data of the image.

 

What line of code was throwing the exception?  What exception is it?

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: getting an image from url - no stack trace error

I agree with Mark.

 

There is a chance that a String is the correct form though.  Whether you want String or data will depend on the format of the image you are receiving.  if the Server has converted it, as some do, to text for example using Base64, then String might be reasonable.  So does your server do anything with the data?

 

 I would change your http code to check the response cod before you start trying to download the data.  Unless you have a good response, then the data you are getting is unlikely to be image data. 

 

I would return a byte array and not a String - the easiest way to do this is read the input Stream using

IOUtilities.streamTobyte()

Review the documentation for this.

 

So I think your call back should have two methods, one for the Exceptions (which returns a String) and one for your image, which returns bytes. 

 

But I think you also have a problem in your WebBitmapField.  Since it extends BitmapField, I think you actually have to create it with a Bitmap - a temporary one - so that it has some size and can paint itself.  When you get the real Bitmap, do a setBitmap to change from the placeholder to the real image. 

 

If you make these changes I hope this will help.  If not, i suggest you step through this code with break points and so determine the line throwing the exception and the some details on the Exception.  That will be helpful.