Note that there are some explanatory texts on larger screens.

plurals
  1. POLoad Bitmap Image From SDCard To ListView Android
    text
    copied!<p>I am trying to load bitmap image to listview from sdcard but imageview is not showing image. </p> <p>Here is my code to get image from SDCard:</p> <pre><code>public static Bitmap getArtwork(Context context, int album_id) { ContentResolver res = context.getContentResolver(); Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id); if (uri != null) { InputStream in = null; try { in = res.openInputStream(uri); return BitmapFactory.decodeStream(in, null, sBitmapOptions); } catch (FileNotFoundException ex) { // The album art thumbnail does not actually exist. Maybe the user deleted it, or // maybe it never existed to begin with. } finally { if (in != null) { try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } return null; } </code></pre> <p>Code to display Image and text in listview :</p> <pre><code>public class AlbumList extends ListActivity{ public final ArrayList&lt;HashMap&lt;String, Object&gt;&gt; songsList = new ArrayList&lt;HashMap&lt;String, Object&gt;&gt;(); public final ArrayList&lt;HashMap&lt;String, Object&gt;&gt; songsListData = new ArrayList&lt;HashMap&lt;String, Object&gt;&gt;(); private Context context; ListAdapter adapter; private static final BitmapFactory.Options sBitmapOptions = new BitmapFactory.Options(); private static final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart"); //Context contentResolver; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list); context = this; final String[] cursor_cols = { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.DURATION }; Cursor cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, cursor_cols, null, null, null); if (cursor == null) { //Query Failed , Handle error. } else if (!cursor.moveToFirst()) { //No media on the device. } else { int albumName = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM); int id = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID); for(int i=0;i&lt;cursor.getCount();i++) { String Name = cursor.getString(albumName); Integer albumid = cursor.getInt(id); Bitmap bitmap = null; bitmap = getArtwork(context, albumid); if(bitmap == null) { bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon); } // ImageView iv = (ImageView) findViewById(R.id.list_image); // iv.setImageBitmap(bitmap); HashMap&lt;String, Object&gt; song = new HashMap&lt;String, Object&gt;(); song.put("albumArt", bitmap); song.put("albumName", Name); // Adding each song to SongList songsList.add(song); cursor.moveToNext(); } } cursor.close(); // looping through playlist for (int i = 0; i &lt; songsList.size(); i++) { // creating new HashMap HashMap&lt;String, Object&gt; song = songsList.get(i); // adding HashList to ArrayList songsListData.add(song); } // Adding menuItems to ListView String[] from = {"albumArt" , "albumName" }; int[] to={R.id.list_image , R.id.albumName}; adapter = new SimpleAdapter(this, songsListData,R.layout.list_item, from, to); setListAdapter(adapter); } </code></pre> <p>I have tried to display single image in imageview which works for same code. But for listview i am not getting image. So what should i do?? Pleaze suggest me some solution. Thanx.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload