[英]How should I set this Bitmap in ListView?
I have images in my local database which I want to populate in a listview. 我的本地数据库中有图像,我想在列表视图中填充这些图像。
This is what I'm doing : 这就是我在做什么:
...
bitmap[i] = MySQLiteHelper.getImage(images[i]);
// This fetches the bitmap image from database
...
Here, I'm converting my Bitmap to a Drawable. 在这里,我将我的位图转换为Drawable。
d[i] = new BitmapDrawable(getResources(),bitmap[i]);
Then I'm using this Drawable in HashMap's put method to set it as a listView item. 然后,我在HashMap的put方法中使用此Drawable将其设置为listView项。
hm.put("img", String.valueOf(d[i]));
// put() accepts java.lang.String as input.
But the image is not displayed in the listview. 但是图像未显示在列表视图中。 I'm able to display all the text, but not the image.
我能够显示所有文本,但不能显示图像。
I'm getting the following error in my LogCat : 我在LogCat中遇到以下错误:
BitmapFactory﹕ Unable to decode stream: java.io.FileNotFoundException: android.graphics.drawable.BitmapDrawable@3ab28e36: open failed: ENOENT (No such file or directory)
resolveUri failed on bad bitmap uri: android.graphics.drawable.BitmapDrawable@3ab28e36
What am I doing wrong here? 我在这里做错了什么? I think that when converting that Bitmap to Drawable, the drawable is holding a temporary value and the put() cannot access that?
我认为将Bitmap转换为Drawable时,drawable持有一个临时值,而put()无法访问该值? What is wrong here folks, any help please?
这里的人怎么了,请帮忙吗?
EDIT : 编辑:
My HashMap code : 我的HashMap代码:
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<len;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("img", String.valueOf(d[i]));
hm.put("tit", " Title : " + title[i]);
hm.put("init", " Initial Price : " + init_price[i]);
hm.put("cur"," Current Price : " + cur_price[i]);
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "img","tit","init","cur" };
// Ids of views in listview_layout
int[] to = { R.id.img,R.id.tit,R.id.init,R.id.cur};
getImage() method : getImage()方法:
The image (Bitmap) is stored in the database as BLOB. 图像(位图)作为BLOB存储在数据库中。 I obtained it as a ByteArray.
我获得它作为ByteArray。 Now, I'm converting it back to Bitmap using the below method.
现在,我使用以下方法将其转换回Bitmap。
// convert from byte array to bitmap
public static Bitmap getImage(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
My Logcat : after using Base64
to convert Bitmap
to String
我的Logcat:使用
Base64
将Bitmap
转换为String
(Note: consider using the new RecyclerView instead of a ListView
for better performance) (注意:考虑使用新的RecyclerView而不是
ListView
以获得更好的性能)
What you are trying to do is either converting a String
to a BitmapDrawable
(in case MySQLiteHelper.getImage()
returns a String
as a path pointing to an image, since usually you won't store binary data in a database) OR converting a BitmapDrawable
to a String
. 您正在尝试做的是将
String
转换为BitmapDrawable
(以防MySQLiteHelper.getImage()
返回String
作为指向图像的路径,因为通常您不会在数据库中存储二进制数据)或转换BitmapDrawable
到一个String
。 Both cases actually make no sense. 两种情况实际上都没有道理。
The way it is usually done: implementing a helper class and then parameterizing your ListView's Adapter with its instances. 通常的方法是:实现一个帮助器类,然后使用其实例对ListView的Adapter进行参数化。 There are literally tons of examples around the web, look at this one , or that one which uses a database
网上确实有无数的示例,看看这个 ,还是那个使用数据库的示例
From the error message it seems the problem is elsewhere in the code. 从错误消息看来,问题出在代码的其他地方。
BitmapFactory﹕ Unable to decode stream: java.io.FileNotFoundException
This probably means you are decoding the file with a location and the location is invalid. 这可能意味着您正在解码具有位置的文件,并且该位置无效。 Perhaps you are using BitmapFactory.decodeFile?
也许您正在使用BitmapFactory.decodeFile?
Check in your code if you are using BitmapFactory and make sure you are passing the correct location if available. 如果您使用的是BitmapFactory,请签入代码,并确保传递正确的位置(如果可用)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.