0

I want to get the Bitmap from URL like -

"https://graph.facebook.com/100003506521332/picture"

I tried how-i-can-display-images-from-url-in-blackberry . But its showing http error 302 .It dont showing the Bitmap. How to resolve the problem ?.

Community
  • 1
  • 1
  • 1
    How to get bitmap from URL in Blackberry. Blackberry is using java language. and i put the link what i tried. –  Mar 20 '12 at 14:21
  • do you mean, you just want to display a .bmp loaded from that URL? – thecoshman Mar 20 '12 at 14:22
  • HTTP response code `302` is a redirection to another URL. The HTTP response's `Location` header contains the new URL to go to. – Remy Lebeau Mar 20 '12 at 22:10

3 Answers3

0
//YOUR PACKAGE NAME
package ...........;
//*************************

import java.io.InputStream;

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

import net.rim.device.api.system.Bitmap;

public class Util_ImageLoader {
    public static Bitmap getImageFromUrl(String url) {
        Bitmap bitmap = null;

        try {
            String bitmapData = getDataFromUrl(url);
            bitmap = Bitmap.createBitmapFromBytes(bitmapData.getBytes(), 0,
                    bitmapData.length(), 1);
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        return bitmap;
    }

    private static String getDataFromUrl(String url) {
        StringBuffer b = new StringBuffer();
        InputStream is = null;
        HttpConnection c = null;

        long len = 0;
        int ch = 0;

        try {
            c = (HttpConnection) Connector.open(url);

            is = c.openInputStream();
            len = c.getLength();
            if (len != -1) {
                for (int i = 0; i < len; i++)
                    if ((ch = is.read()) != -1) {
                        b.append((char) ch);
                    }
            } else {
                while ((ch = is.read()) != -1) {
                    len = is.available();
                    b.append((char) ch);
                }
            }

            is.close();
            c.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return b.toString();
    }
}

copy this to your package.... Now, to use this class do like this>>>>>>>>

CREATE BITMAP OBJECT:

Bitmap IMAGE;

After That:

IMAGE=Util_ImageLoader.getImageFromUrl("https://graph.facebook.com/100003506521332/picture");

By the way, I think you have missed extension like JPG,PNG, on your URL.... Check That

Now, add your bitmap where you would like to display the image......

ENJOY!

add(IMAGE);
techsaint
  • 752
  • 9
  • 22
Dayslight
  • 26
  • 4
0
String url = "https://graph.facebook.com/100003506521332/picture";
     try 
        {
            bitmap = globleDeclaration.getLiveImage(url, 50, 50);
            bitmapField.setBitmap(bitmap);
        }
        catch (IOException e) 
        {
            e.printStackTrace();
            Dialog.alert(e.getMessage());
        }





public Bitmap getLiveImage(String url,int width,int height) throws IOException
    {   
        Bitmap bitmap = null;
try  
        {  
             byte[] responseData = new byte[10000];  
               int length = 0;  
               StringBuffer rawResponse = new StringBuffer();  
            httpconnection = (HttpConnection) Connector.open(url, Connector.READ, true);  
            String location=httpconnection.getHeaderField("location");

                if(location!=null){
                     httpconnection = (HttpConnection) Connector.open(location, Connector.READ, true);  

                }else{
                     httpconnection = (HttpConnection) Connector.open(url, Connector.READ, true);  
                }


                inputStream = httpconnection.openInputStream();  


                    while (-1 != (length = inputStream.read(responseData)))  
                    {  
                     rawResponse.append(new String(responseData, 0, length));  
                    }  
                    int responseCode = httpconnection.getResponseCode();


                if (responseCode != HttpConnection.HTTP_OK)  
                {  
                    throw new IOException("HTTP response code: "  
                            + responseCode);  
                }  
                final String result = rawResponse.toString();

                 byte[] dataArray = result.getBytes();  
                 encodeImageBitmap = EncodedImage.createEncodedImage(dataArray, 0, dataArray.length); 

            }  
            catch (final Exception ex)  
            {  
             System.out.print("Exception (" + ex.getClass() + "): " + ex.getMessage());   
            }  
            finally  
            {  
                try  
                {  
                    inputStream.close();  
                    inputStream = null;  
                    httpconnection.close();  
                    httpconnection = null;  
                }  
                catch(Exception e){}  
            }


        return bitmap;
    }
Rince Thomas
  • 4,158
  • 5
  • 25
  • 44
  • @Signare: you are using a `StringBuffer` to receive binary data, just to convert it back to aa binary `byte[]` array, why? The connection's `InputStream` reads the raw binary data, using a `String` is overkill. I would use `HttpConnection.getLength()` to get the binary data length, allocate a `byte[]` of that length, and then `read()` directly into it. The documentation for `HttpConnection` even shows an example of doing exactly that. – Remy Lebeau Mar 21 '12 at 06:22
  • @Signare: You are also re-opening the same URL again if no `Location` header is present. That is not necessary. – Remy Lebeau Mar 21 '12 at 06:23
0

If you done get responce from this code httpconnection = (HttpConnection) Connector.open(url, Connector.READ, true); which is mention in above answer than u can also use alternative solution for that..

You can pass your your id here http://graph.facebook.com/100003506521332/?fields=picture&type=large than you will get one json responce like that {"picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/70678_100003506521332_1415569305_n.jpg"}

parse this json and get which you want exaclty .. than you will get responce using httpconnection = (HttpConnection) Connector.open(url, Connector.READ, true); also .

if you dont want larger image than remove &type=large from the above url.

Hitarth
  • 1,950
  • 3
  • 27
  • 52