Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use BitmapFactory.decodeFile method to decode a image from http location?

I would like to know if it is possible to use BitmapFactory.decodeFile method to decode a image from http location?

For eg.

ImageView imageview = new ImageView(context);
Bitmap bmp = BitmapFactory.decodeFile("http://<my IP>/test/abc.jpg");  
imageview.setImageBitmap(bmp);

But bmp is always returning null.

Is there any other way to achieve this scenario, where i have a set of images in my server PC, and i am loading the images to my gallery application via an xml?

Thanks,
Sen

like image 345
Navaneeth Sen Avatar asked Dec 22 '10 14:12

Navaneeth Sen


2 Answers

Use decodeStream and pass the URL's inputstream instead.

Here is an example:

Bitmap bmp = BitmapFactory.decodeStream(new java.net.URL(url).openStream())
like image 105
Amir Raminfar Avatar answered Nov 15 '22 20:11

Amir Raminfar


@Amir & @Sankar : Thanks for your valuable suggestions.

I solved the above problem by doing the following code snippet :

ImageView iv = new ImageView(context);

try{
    String url1 = "http://<my IP>/test/abc.jpg";
    URL ulrn = new URL(url1);
    HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
    InputStream is = con.getInputStream();
    Bitmap bmp = BitmapFactory.decodeStream(is);
    if (null != bmp)
        iv.setImageBitmap(bmp);
    else
        System.out.println("The Bitmap is NULL");

} catch(Exception e) {
}

Thanks,
Sen

like image 29
Navaneeth Sen Avatar answered Nov 15 '22 19:11

Navaneeth Sen