首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取AlbumArt时出现内存不足异常

获取AlbumArt时出现内存不足异常
EN

Stack Overflow用户
提问于 2012-12-28 15:45:08
回答 1查看 513关注 0票数 0

在这里,我已经创建了可扩展的列表视图,其中有艺术家的组行,在孩子中,它显示了艺术家的所有专辑与专辑艺术。但是它抛出内存异常。

下面是我获取图像的代码:

代码语言:javascript
复制
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;
    }

为了创建子专辑列表,我这样做了:

代码语言:javascript
复制
private ArrayList<ArrayList<HashMap<String, Object>>> createChildList() {
    for (int i = 0; i < songsListData.size(); i++) 
    {

        ArrayList<HashMap<String,Object>> secList = new ArrayList<HashMap<String,Object>>();
        String s[] = new String[]{songsListData.get(i).get("artistname")};
        String whereClause = MediaStore.Audio.Albums.ARTIST + " = ? ";

        Cursor cursor = managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,null ,whereClause,s,null);

        if (cursor == null) 
        {
              //Query Failed , Handle error.
        }
        else if (!cursor.moveToFirst()) 
        {
             //No media on the device.
        }
        else
        {            
              int albumName = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
              int id = cursor.getColumnIndex(MediaStore.Audio.Albums._ID);
              int songcount = cursor.getColumnIndex(MediaStore.Audio.Albums.NUMBER_OF_SONGS);

              for(int j=0;j<cursor.getCount();j++)
              {
                    String Name = cursor.getString(albumName);
                    Integer albumid = cursor.getInt(id);                        
                    Bitmap bitmap = null;
                    bitmap = getArtwork(context, albumid); //calling function
                    if(bitmap == null)
                    {
                        bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.wallpaper);
                    }   
                    HashMap<String, Object> album = new HashMap<String, Object>();
                    album.put("albumName",Name);
                    album.put("albumId", albumid);  
                    album.put("image", bitmap);  //storing image
                    album.put("songcount",cursor.getString(songcount) + " song");
                    secList.add(album);
                    cursor.moveToNext();
               }                  
           }
           cursor.close();   
           albumData.add(secList);
    }
    return albumData;
}

我应该如何处理内存不足异常。??请给我一些解决方案。谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-28 15:50:40

调试代码并跟踪堆大小当堆大小意外增加时,这一定是由于图像大小的增加。您可以使用Document来了解Android如何计算图像大小

代码语言:javascript
复制
public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) {
    int width = image.getWidth();
    int height = image.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height,
            matrix, false);
    return resizedBitmap;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14066331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档