7

ビットマップを使用しています。コードを実行すると、メモリ不足エラーが表示されます。どうすればエラーを回避できますか。私のコードは次のとおりです。前もって感謝します。

Bitmap myBitmap = Image.decodeSampledBitmapFromUri(path, 250, 500); 
img_cook[index].setImageBitmap(myBitmap); 

public static Bitmap decodeSampledBitmapFromUr(String path, int reqWidth,
            int reqHeight) {

    Bitmap bm = null;

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    bm = BitmapFactory.decodeFile(path, options);

    return bm;
}

public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
         inSampleSize = Math.round((float)height / (float)reqHeight);    
        } else {
         inSampleSize = Math.round((float)width / (float)reqWidth);    
        }   
       }
4

4 に答える 4

8

Bitmap の処理が完了したら、Bitmap の処理が完了したら、以下のようにリサイクルして null にすることを意味します。

bitmap.recycle();
bitmap=null;

また

URL から画像をダウンロードしていると思うので、これには Android Query を使用することをお勧めします。これを使用すると、このエラーが発生することはありません。

ここから jar ファイルをダウンロードできます: http://code.google.com/p/android-query/downloads/list jar ファイルをダウンロードし、jar をビルド パスに設定します。

 AQuery androidAQuery=new AQuery(this);

URL から直接画像をロードする例として:

androidAQuery.id(YOUR IMAGEVIEW).image(YOUR IMAGE TO LOAD, true, true, getDeviceWidth(), ANY DEFAULT IMAGE YOU WANT TO SHOW);

URL からビットマップを取得する例として:

androidAQuery.ajax(YOUR IMAGE URL,Bitmap.class,0,new AjaxCallback<Bitmap>(){
    @Override
    public void callback(String url, Bitmap object, AjaxStatus status) {
        super.callback(url, object, status);

        //You will get Bitmap from object.
    }
});

これは非常に高速で正確です。これを使用すると、読み込み時にアニメーションなどの多くの機能を見つけることができます。必要に応じてビットマップを取得します。等

于 2013-10-03T06:58:48.267 に答える