13

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

i have a problem in set url image to image view. i tried below methods

Method 1:

Bitmap bimage=  getBitmapFromURL(bannerpath);
image.setImageBitmap(bimage);

 public static Bitmap getBitmapFromURL(String src) {
        try {
            Log.e("src",src);
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            Log.e("Bitmap","returned");
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("Exception",e.getMessage());
            return null;
        }
    }

Method 2:

    Drawable drawable = LoadImageFromWebOperations(bannerpath);
    image.setImageDrawable(drawable);

     private Drawable LoadImageFromWebOperations(String url)
    {
         try
         {
             InputStream is = (InputStream) new URL(url).getContent();
             Drawable d = Drawable.createFromStream(is, "src name");
             return d;
         }catch (Exception e) {
             System.out.println("Exc="+e);
             return null;
         }
     }

i tried above two methods but are not working . both are showing DEBUG/skia(266): --- decoder->decode returned false . i used demo url image paths that is working. but this path is not working .so please tell me what is the wrong and what i will do

Thank you in advance.

Best Regards.

Community
  • 1
  • 1
Ramakrishna
  • 4,066
  • 16
  • 48
  • 72

1 Answers1

8

Just Tested your "Method 1" in my app and it works just fine.

You might have forgotten this line in your AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET" />
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • Method 1 works fine with the INTERNET permission on a ImageView using setImageBitmap(bimage). – chirale Jul 12 '12 at 09:34
  • 1
    My application crashes constantly with the `android.os.NetworkOnMainThreadException` exception (After adding the INTERNET permission to the manifest). What am I doing wrong? – Jonathan Perry Aug 01 '13 at 05:49
  • NetworkOnMainThreadException shouldn't crash, its just informing you that your doing network communication on the UI thread. – Deminetix Aug 29 '13 at 06:12
  • @Deminetix It's an Exception. Of course it crashes! Anyway, this can be fixed by loading the image in an `AsyncTask` (and displaying the image in `onPostExecute()` – Navin Oct 30 '13 at 22:46