我正在从 URL 下载图像并将此位图用于我的布局背景。我看不到我的图像。
过去两天我一直在努力解决这个问题。现在我仍然无法找到解决方案。我用谷歌搜索了很多。请帮助我犯下的错误。
提前非常感谢。
我的代码:
Bitmap myImage = getBitmapFromURL("http://looksok.files.wordpress.com/2011/12/me.jpg");
if (android.os.Build.VERSION.SDK_INT >= 16){
setBackgroundV16Plus( myImage);
}
else{
setBackgroundV16Minus( myImage);
}
@TargetApi(16)
private void setBackgroundV16Plus( Bitmap bitmap) {
linearLayout.setBackground(new BitmapDrawable(getApplicationContext().getResources(), bitmap));
}
@SuppressWarnings("deprecation")
private void setBackgroundV16Minus( Bitmap bitmap) {
linearLayout.setBackgroundDrawable(new BitmapDrawable(bitmap));
}
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;
}
}
最佳答案
RelativeLayout relative = (RelativeLayout) findViewById(R.id.relative1);
Drawable dr = new BitmapDrawable(bit);
(view).setBackgroundDrawable(drawable);
关于android - 将位图设置为我的布局背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37383426/