【问题标题】:How to set background for an imageView from an image URL? [duplicate]如何从图像 URL 为 imageView 设置背景? [复制]
【发布时间】:2012-01-19 05:25:39
【问题描述】:

可能重复:
how to load an imageview by url in android?

我有一个图像 URL。我想将该图像设置为我的 ImageViews 背景。 请帮忙..

【问题讨论】:

    标签: android


    【解决方案1】:

    希望这段代码对你有帮助。

      public static Bitmap loadBitmap(String url) {
        Bitmap bitmap = null;
        InputStream in = null;
        BufferedOutputStream out = null;
    
        try {
            in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
    
            final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
            out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
            copy(in, out);
            out.flush();
    
            final byte[] data = dataStream.toByteArray();
            BitmapFactory.Options options = new BitmapFactory.Options();
            //options.inSampleSize = 1;
    
            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
        } catch (IOException e) {
            Log.e(TAG, "Could not load Bitmap from: " + url);
        } finally {
            closeStream(in);
            closeStream(out);
        }
    
        return bitmap;
    }
    

    然后使用imageView.setImageBitmap(bitmap);

    【讨论】:

      【解决方案2】:

      查看此链接How to display image from URL on Android

      然后,您可以将 setBackgroundDrawable() 与从 URL 创建的可绘制对象一起使用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-27
        • 1970-01-01
        • 2021-03-14
        • 1970-01-01
        • 1970-01-01
        • 2013-06-19
        相关资源
        最近更新 更多