从SDCard加载位图图像到ListView Android

时间:2012-12-04 07:20:10

标签: android android-listview android-arrayadapter android-mediaplayer mediastore

我正在尝试从sdcard将位图图像加载到listview,但是imageview没有显示图像。

以下是我从SDCard获取图片的代码:

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;
    }

在listview中显示图像和文本的代码:

public class AlbumList extends ListActivity{

    public final ArrayList<HashMap<String, Object>> songsList = new ArrayList<HashMap<String, Object>>();
    public final ArrayList<HashMap<String, Object>> songsListData = new ArrayList<HashMap<String, Object>>();
    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<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<String, Object> song = new HashMap<String, Object>();
                            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 < songsList.size(); i++) {
                    // creating new HashMap
                    HashMap<String, Object> 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);
}

我试图在imageview中显示单个图像,它适用于相同的代码。但对于listview我没有得到图像。所以我该怎么做?? Pleaze建议我解决一些问题。 感谢名单。

2 个答案:

答案 0 :(得分:0)

您可以使用LazyList项目来实现您的要求。

这是同问的问题here 希望这可能会有所帮助:)

答案 1 :(得分:0)

ListViews会夸大你告诉他们的内容。您应该创建一个xml布局并将其命名为list_item,在此处按照您希望的方式排列ImageView和Textview。确保设置包含布局的高度以包装内容。

现在,当您在列表视图中对视图进行夸大时,请对您所做的项目进行充气。然后将此膨胀布局中的textview和imageview设置为您想要的内容。

例如,在listview适配器中执行与此类似的操作。

@Override
  public View getView(int position, View convertView, ViewGroup parent) {

 if(convertView==null){
   convertView = li.inflatelayout(R.layout.list_item);

    }
    MyListItemModel item = items.get(position);
     // replace those R.ids by the ones inside your custom list_item layout.
     TextView label = (TextView)convertView.findViewById(R.id.item_textview);
     label.setText(text);
     ImageView iv= (Imageview)convertView.findViewById(R.id.item_imageview);
    iv.setsourceimage(bitmap);

    return convertView;
}

如果需要,你也可以在这里设置他们的onclick听众。