【问题标题】:Out Of Memory Exception While Getting AlbumArt获取 AlbumArt 时出现内存不足异常
【发布时间】:2012-12-28 07:45:08
【问题描述】:

在这里,我创建了可扩展的列表视图,其中包含艺术家的组行,并在子项中显示艺术家的所有专辑以及专辑封面。但它会抛出内存不足异常。

这是我获取图像的代码:

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

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

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

我应该怎么做才能处理内存不足异常。??请给我一些解决方案。谢谢。

【问题讨论】:

    标签: android bitmap out-of-memory expandablelistview bitmapfactory


    【解决方案1】:

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

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

    【讨论】:

    • 在存储到hashmap之前我应该​​减少位图图像的高度和宽度吗??
    • 是的,首先阅读文档,然后调试您的代码并检查堆大小,然后如果您需要减小位图的大小,您可以使用上面的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 2017-05-13
    • 2011-06-18
    • 2015-02-19
    相关资源
    最近更新 更多