Android 将位图设置为我的布局背景
我正在从URL下载图像,并将此位图用于我的布局背景。我看不见自己的形象 在过去的两天里,我一直在努力解决这个问题。现在我仍然找不到解决办法。我在谷歌上搜索了很多。请帮我搞错我做的事 先谢谢你 我的代码:Android 将位图设置为我的布局背景,android,android-bitmap,setbackground,Android,Android Bitmap,Setbackground,我正在从URL下载图像,并将此位图用于我的布局背景。我看不见自己的形象 在过去的两天里,我一直在努力解决这个问题。现在我仍然找不到解决办法。我在谷歌上搜索了很多。请帮我搞错我做的事 先谢谢你 我的代码: Bitmap myImage = getBitmapFromURL("http://looksok.files.wordpress.com/2011/12/me.jpg"); if (android.os.Build.VERSION.SDK_INT >= 16){
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;
}
}
要连接到internet,必须使用Thrad或AsynkTask
public class MyAyncTask extends AsyncTask<Void, Void,Void>{
@Override
protected Void doInBackground(Void... params) {
Bitmap myImage = getBitmapFromURL("http://looksok.files.wordpress.com/2011/12/me.jpg");
return null;
}
}
公共类MyAyncTask扩展了AsyncTask{
@凌驾
受保护的Void doInBackground(Void…参数){
位图myImage=getBitmapFromURL(“http://looksok.files.wordpress.com/2011/12/me.jpg");
返回null;
}
}
要连接到internet,必须使用Thrad或AsynkTask
public class MyAyncTask extends AsyncTask<Void, Void,Void>{
@Override
protected Void doInBackground(Void... params) {
Bitmap myImage = getBitmapFromURL("http://looksok.files.wordpress.com/2011/12/me.jpg");
return null;
}
}
公共类MyAyncTask扩展了AsyncTask{
@凌驾
受保护的Void doInBackground(Void…参数){
位图myImage=getBitmapFromURL(“http://looksok.files.wordpress.com/2011/12/me.jpg");
返回null;
}
}
使用毕加索图书馆它很容易使用
这里是毕加索例子的链接
使用毕加索图书馆它很容易使用 这里是毕加索例子的链接 此语句下载图像并存储到位图变量,现在设置背景 并在清单上添加Internet权限 此语句下载图像并存储到位图变量,现在设置背景
并在清单上添加Internet权限。如果您收到任何例外,请在此处提及第一点请求是https而不是http。还添加
connection.setRequestMethod(“GET”)
在连接之前。connect()代码>获取请求行如果您获取任何例外,请在此处提及第一点请求是https而不是http。还添加connection.setRequestMethod(“GET”)
在连接之前。connect()private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
public LoadProfileImage() {
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap bitmap = null;
try {
InputStream in = new URL(urldisplay).openStream();
bitmap = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.println(Log.ASSERT, "error", "" + e);
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap result) {
myImage=result;
}
}
new LoadProfileImage().execute("http://looksok.files.wordpress.com/2011/12/me.jpg");