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을 사용할 수 있습니다 아이디어를 얻을 이미지 크기 증가로 인한해야한다는 까지 예기치 않게가는 코드 및 추적 힙 크기 안드로이드는 이미지 크기를 계산하는 방법을 디버그 그

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

내가해야 해시 맵에 저장하기 전에 비트 맵 이미지의 높이와 너비를 줄입니까 ?? – zanky

+0

예 첫번째로 문서를 읽은 다음 코드를 디버그하고 힙 크기를 확인하십시오. 비트 맵의 ​​크기를 줄이면 위의 코드를 사용할 수 있습니다. –

+0

ohk thanx..i 시도 중입니다. – zanky

관련 문제