如何使用Volley或其他设置从URL设置相对布局背景图像?

时间:2015-10-15 08:55:32

标签: android url layout

我想从网址设置布局背景图片

使用凌空或毕加索或其他一些

有没有办法设置这个!

我在网上找到了以下代码但没有工作!!

Bitmap myImage = getBitmapFromURL("http://looksok.files.wordpress.com/2011/12/me.jpg");
RelativeLayout rLayout=(RelativeLayout)findViewById(R.id.relativeLayout);

//BitmapDrawable(obj) convert Bitmap object into drawable object.
Drawable dr = new BitmapDrawable(myImage);
rLayout.setBackgroundDrawable(dr);

public Bitmap getBitmapFromURL(String imageUrl) {
    try {
        URL url = new URL(imageUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

3 个答案:

答案 0 :(得分:0)

请尝试使用此Android智能图像视图库。

http://loopj.com/android-smart-image-view/

答案 1 :(得分:0)

我使用以下链接来设置来自网址的图片: https://android-arsenal.com/details/1/211

答案 2 :(得分:0)

如果您使用的是android studio,请将compile 'com.squareup.picasso:picasso:2.5.2'放入build.gradle文件并编译项目。

然后你可以使用Picasso库

Picasso.with(mContext)
    .load("http://looksok.files.wordpress.com/2011/12/me.jpg")
    .into(rLayout);

这会将来自给定网址的图片加载到rLayout

参考:http://square.github.io/picasso/