0

在這裏,我創建了可擴展的列表視圖,其中有藝術家的組行,並在小孩它顯示專輯藝術家與專輯藝術的所有專輯。但它會拋出內存異常。內存溢出異常雖然獲取AlbumArt

這裏是我的代碼來獲取圖像:

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

我應該怎麼做才能處理內存溢出異常? ?請讓我解決一些問題。感謝名單。

回答

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

應該怎麼在存儲到hashmap之前減少位圖圖像的高度和寬度? – zanky

+0

是第一次閱讀文件,然後調試你的代碼,並檢查堆大小後,如何增加,如果你需要減少位圖的大小,你可以使用上面的代碼 –

+0

ohx thanx ..我正在嘗試。 – zanky