我想顯示的默認PIC當結果爲空,這裏是我的嘗試:如何在Android中設置列表中的默認圖片?
getBitmapFromURL
和使用它的類:
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;
}
}
}
class RetrievePic extends AsyncTask<String, String, Bitmap>{
@Override
protected Bitmap doInBackground(String... urls) {
String url = urls[0];
return JSONCommands.getBitmapFromURL(url);
}
}
執行類ListAdapter
:
// tasvirlimg.setImageResource(R.drawable.p3_books);
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](https://github.com/nostra13/Android-Universal-Image-Loader)或[Picasso](http://square.github.io/picasso/)下載你的圖像,並設置一個默認圖像到你的佔位符。 – 2014-11-24 23:08:21
我想知道人們來到這裏,編輯問題等得到星星!請求只是幫助我的概率! – 2014-11-24 23:09:38
@MohammadRahchamani坦克你,但你可以幫我用我自己的代碼?! – 2014-11-24 23:10:45