将url图像设置为图像视图

Ram*_*hna 13 url android imageview

可能重复:
是否可以使用BitmapFactory.decodeFile方法解码来自http位置的图像?

我在设置网址图像到图像视图时遇到问题.我试过以下方法

方法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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

方法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;
         }
     }
Run Code Online (Sandbox Code Playgroud)

我尝试了两种方法,但没有工作.两者都显示DEBUG/skia(266):--- decoder-> decode返回false.我使用了正在运行的demo url图像路径.但这条路不起作用.请告诉我有什么不对,我会做什么

先感谢您.

最好的祝福.

小智 8

刚刚在我的应用中测试了你的"方法1",它运行得很好.

您可能忘记了AndroidManifest.xml文件中的这一行:

<uses-permission android:name="android.permission.INTERNET" />
Run Code Online (Sandbox Code Playgroud)