Nikhil
Nikhil

Reputation: 6643

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.

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.

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 :

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? What is wrong here folks, any help please?

EDIT :

My HashMap code :

 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 :

The image (Bitmap) is stored in the database as BLOB. I obtained it as a ByteArray. Now, I'm converting it back to Bitmap using the below method.

 // 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

enter image description here

Upvotes: 0

Views: 894

Answers (2)

ZakiMak
ZakiMak

Reputation: 2102

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?

Check in your code if you are using BitmapFactory and make sure you are passing the correct location if available.

Upvotes: 1

Droidman
Droidman

Reputation: 11608

(Note: consider using the new RecyclerView instead of a ListView for better performance)

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. 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. There are literally tons of examples around the web, look at this one, or that one which uses a database

Upvotes: 1

Related Questions