set url image to image view [duplicate]

2020-02-10 17:43发布

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.

1条回答
唯我独甜
2楼-- · 2020-02-10 18:03

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" />
查看更多
登录 后发表回答