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
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,723
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.
Found a bug? Report it using Issue Tracker
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.