2

I want to convert the (R.drawable.logo)object to a string and I want to show it in the listview. I used the following code for it but it did not work

 Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
 ByteArrayOutputStream ByteStream = new  ByteArrayOutputStream();
 bitmapOrg.compress(Bitmap.CompressFormat.PNG,100, ByteStream);
 byte [] b = ByteStream.toByteArray();
 String temp = Base64.encodeToString(b, Base64.DEFAULT);

 ListAdapter adapter = new SimpleAdapter(
     Welcome.this,
     contactList,
     R.layout.about,
     new String[] { TAG_NAME, TAG_SURNAME, temp },
     new int[] { R.id.name, R.id.surname, R.id.imageView}
 );

 list.setAdapter(adapter);

ERROR: Unable to decode stream: java.io.FileNotFoundException: : open failed: ENOENT (No such file or directory)

Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55
Alpaslan
  • 17
  • 1
  • 6

1 Answers1

0

To display the image in ListView using SimpleAdapter you don't need to convert your image to a Base64 string.

Change new String[] { TAG_NAME, TAG_SURNAME, temp } to new String[] { TAG_NAME, TAG_SURNAME, TAG_IMAGE }.

Inside your contactList array add the image as follows,

for(int i = 0; i < length; i++){
    HashMap<String, String> hm = new HashMap<String,String>();
    hm.put(TAG_NAME, names[i]);
    hm.put(TAG_SURNAME, surnames[i]);
    hm.put(TAG_IMAGE, Integer.toString(R.drawable.logo) );
    contactList.add(hm);
}

This will set the R.drawable.logo for all rows.

But the error you are getting is a FileNotFoundException. So also cross check if the image exists too.

Check this SO thread.

Community
  • 1
  • 1
K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33