0

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

Nikhil
  • 6,493
  • 10
  • 31
  • 68
  • 1
    It won't solve your problem but why a `HashMap` with `String` to contain `BitmapDrawable` why not `HashMap` – Laurent Meyer Mar 16 '15 at 16:47
  • But to solve your bug I'll ensure that I get something from my DB and that I get no exception when creating the `BitmapDrawable` (debugger is your friend ;) ). – Laurent Meyer Mar 16 '15 at 16:53
  • Haha! Thanks @LaurentMeyer. Ya! Why HashMap because I also have other Strings to pass to the HashMap. – Nikhil Mar 16 '15 at 16:58

2 Answers2

1

(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

Droidman
  • 11,485
  • 17
  • 93
  • 141
  • getImage() returns a BitMap. Pl refer to my getImage() method in question . – Nikhil Mar 16 '15 at 18:14
  • @nikhil than it's the second case I mentioned `String.valueOf(d[i])`. Again - you can't convert binary data to `String` just like that. If you absolutely feel you need to do it - use something like `Base64`. See http://stackoverflow.com/questions/13562429/how-many-ways-to-convert-bitmap-to-string-and-vice-versa – Droidman Mar 16 '15 at 18:41
  • Thanks @Droidman. I used Base64 to convert my bitmap to String and now I'm no longer getting that error. Of course , I'm still not getting the image displayed, but I feel that has got something to do with other aspects. – Nikhil Mar 16 '15 at 19:06
  • The app has become too slow and the logcat is showing that it is processing too much output. Last time my app had become slow when it had to display images. Can u throw some light on that? Please look at my logcat in question. Thanks for your time. – Nikhil Mar 16 '15 at 19:07
  • 1
    @nikhil of course it will be slow.. What you are doing now is: taking a binary file from the database, converting it into a `Bitmap`, then into a `BitmapDrawable`, then encoding it as a `Base64 String` and finally decoding again and displaying. This is a huge CPU/RAM/battery impact. The correct way is: saving the actual image to device storage, writing its *path* into the database, and then using this path to load the image in the `getView` method of your Adapter. Take a look at the `Picasso` library which can handling image loading for you – Droidman Mar 16 '15 at 19:22
  • Thanks a ton @Droidman. I followed your suggestion. I stored the image in device storage and passed the path and guess what, now my app is blazing fast. Thanks a lot!! :) – Nikhil Mar 17 '15 at 16:26
1

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.

ZakiMak
  • 2,072
  • 2
  • 17
  • 26