【发布时间】:2014-11-24 23:05:09
【问题描述】:
我想在结果为空时显示默认图片,这是我的尝试:
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
connection.disconnect();
return myBitmap;
} catch (IOException e) {
// tasvirdimg.setImageResource(R.drawable.p3_books);
e.printStackTrace();
return null;
}
}
}
使用getBitmapFromURL的类:
class RetrievePic extends AsyncTask<String, String, Bitmap>{
@Override
protected Bitmap doInBackground(String... urls) {
String url = urls[0];
return JSONCommands.getBitmapFromURL(url);
}
}
在ListAdapter 中执行RetrievePic 类:
RetrievePic retPic = new RetrievePic();
retPic.execute(JSONCommands.firstURL + MainActivity.books_array.get(position).tasvir);
Bitmap img = null;
try {
img = retPic.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
tasvirlimg.setImageResource(R.drawable.p3_books);
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
tasvirlimg.setImageResource(R.drawable.p3_books);
e.printStackTrace();
}
tasvirlimg.setImageBitmap(img);
当图片为空时,它不显示任何内容,但应该显示默认图片R.drawable.p3_books。感谢您的帮助。
【问题讨论】:
-
您可以使用Universal Image Loader 或Picasso 下载您的图片,并为您的占位符设置默认图片。
-
我想知道人们来这里编辑问题等以获得星星!!!!请帮我解决我的问题!
-
@MohammadRahchamani 坦克你,但你能帮我用我自己的代码吗?!!
-
我认为你应该在 if/else 语句中设置默认图片而不是 catch 块。
-
@MohammadRahchamani 是的。我这样做了,但没有用。
标签: android listview bitmap imageview drawable