Java Android通过url设置ImageButton背景
我试图通过url向ImageButton添加背景图像 我修改了google android文档中的downloadimagetask,如下所示Java Android通过url设置ImageButton背景,java,android,android-asynctask,imagebutton,Java,Android,Android Asynctask,Imagebutton,我试图通过url向ImageButton添加背景图像 我修改了google android文档中的downloadimagetask,如下所示 class DownloadImageButtonTask extends AsyncTask<String, Void, Bitmap> { ImageView bmImage; public DownloadImageButtonTask(ImageButton bmImage) {
class DownloadImageButtonTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageButtonTask(ImageButton bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
设置
尝试设置
if( e.getMessage()!=null)
{
Log.e("Error", e.getMessage());
e.printStackTrace();
}
或者干脆
String exception= (e.getMessage()!=null)?ex.getMessage():"Exception occured";
Log.e("Error", exception);
logcat错误消失,应用程序不会强制关闭,但背景不会显示在imagebutton上。@user1910290请确保您正在接收位图且不为空。最好使用
UniversalImageDownloader
下载图像。
String exception= (e.getMessage()!=null)?ex.getMessage():"Exception occured";
Log.e("Error", exception);