我有一个存储Bitmaps的HashMap的ArrayList,但是当我尝试用ListView适配器显示它时,它显示了这个错误(是的,我绝对相信Uri是正确的并链接到图像):
08-01 14:03:53.103: E/BitmapFactory(7225): Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap@656c2d18: open failed: ENOENT (No such file or directory)
08-01 14:03:53.103: I/System.out(7225): resolveUri failed on bad bitmap uri: android.graphics.Bitmap@656c2d18
这是我的代码:
ArrayList<HashMap<String, Bitmap>> imgList = new ArrayList<HashMap<String, Bitmap>>();
for(int i = 0; i < jData.length(); i++) {
JSONObject c = jData.getJSONObject(i);
//get the imageURL and set it to string
JSONObject images = c.getJSONObject(TAG_IMAGES);
JSONObject thumbnail = images.getJSONObject(TAG_THUMBNAIL);
String imageURL = thumbnail.getString(TAG_URL);
//Decode Bitmap before putting in HashMap
try {
InputStream in = new java.net.URL(imageURL).openStream();
pic = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
//create new HashMap
HashMap<String, Bitmap> bitmap=new HashMap<String, Bitmap>();
//put HashMap in the ArrayList
bitmap.put(TAG_PIC, pic);
imgList.add(bitmap);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
// and finally display them in ImageView
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
ActivityMain.this, imgList,
R.layout.list_item, new String[] { TAG_PIC },
new int[] { R.id.ivThumb
});
setListAdapter(adapter);
}
我也尝试使用for循环遍历所有lv项目并设置imageViews,但是使用此代码它只设置第一项的ImageView(但循环遍历所有图像)
for(childIndex = 0; childIndex < lv.getAdapter().getCount(); childIndex++) {
// lv.setSelection(childIndex);
runOnUiThread(new Runnable() {
@Override
public void run() {
// childView = lv.getChildAt(childIndex);
//lv.setSelection(childIndex);
iv = (ImageView) findViewById(R.id.ivThumb);
iv.setImageBitmap(pic);
}
});
}
所以如果你能帮我做这个相当简单但有点混乱的事情,那就太好了! (我更喜欢使用hashmap,因为我更容易理解,但任何有效的方法都会很棒!)
编辑:添加了FOR循环并更正了HashMap语句放置。