2012-07-08 9 views
13

私のコードのコードは次のとおりです。この後MediaStore.Images.Media.getBitmapとメモリエラーのうち、

public Bitmap loadPhoto(Uri uri) { 
    Bitmap scaled = null; 
    try { 
    scalled = Bitmap.createBitmap(
     MediaStore.Images.Media.getBitmap(getContentResolver(), uri), 
     0,0,90, 90); 

    if (scaled == null) { return null; } 
    } catch(Exception e) { } 
    return scaled; 
} 

。 ImageViewで拡大表示します。すべての画像はデバイスカメラから取り込まれます。

毎回、私はカメラから3枚の写真を表示した後、エラー:をメモリから外してと表示されます。これを解決するには?

+1

、このリンクをチェックアウト: [http://tutorials-android.blogspot.co.il/2011/11/をoutofmemory-exception-when-decoding.html](http://tutorials-android.blogspot.co.il/2011/11/outofmemory-exception-when-decoding.html) – zwebie

+1

ビットマップスケーリングに関するChet Haaseの素敵なチュートリアルもご覧ください: http://www.youtube.com/watch?v=12cB7gnL6po –

+0

注意してください***このページは日付***から非常にです - 今日あなたはこれを行うだけです:http://stackoverflow.com/a/24135522/ 294884 – Fattie

答えて

1

MediaStore.getBitmap方法は、ビットマップを取得するときにサンプルサイズを指定していない便利な方法であるに修正するBitmapFactoryを使用してみてください。 getBitmap(ContentResolver、Uri)を使用していて、サンプルサイズを使用する場合は、ContentResolverを使用して入力ストリームを取得し、通常と同じようにビットマップをデコードします(まずサンプルサイズを計算し、サンプルサイズ)。

コードサンプルを探している人のために
+1

良いアプローチのように聞こえますが、そのコードはより良い回答になるでしょう – weston

+0

@weston Googleは[link](http://developer.android.com/training/displaying-bitmaps/load-bitmap.html)でこのサンプルコードを提供しています(ビットマップを効率的に読み込むことについて)。ContentResolverの使用方法は、 MediaStore.getBitmapの実装で。それは同じ方法でコンテンツリゾルバを呼び出します –

+1

回答が不完全であるため、私はあなたが他の場所で回答を見つけることができます。この答えを読んだ人は誰も同じ足の仕事をする必要があります。良いスタックオーバーフローの答えは、それを完了するために外部リンクに依存していません。 – weston

2

:私は同じ問題を抱えていた

private static int calculateInSampleSize(
     BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 

     final int halfHeight = height/2; 
     final int halfWidth = width/2; 

     // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
     // height and width larger than the requested height and width. 
     while ((halfHeight/inSampleSize) >= reqHeight 
       && (halfWidth/inSampleSize) >= reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 

    return inSampleSize; 
} 

public static Bitmap decodeSampledBitmapFromUri(Context context, Uri imageUri, int reqWidth, int reqHeight) throws FileNotFoundException { 

    // Get input stream of the image 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    InputStream iStream = context.getContentResolver().openInputStream(imageUri); 

    // First decode with inJustDecodeBounds=true to check dimensions 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeStream(iStream, null, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeStream(iStream, null, options); 
} 
+0

本当にこの機能を自分で試しましたか?私はこれを試して、関数decodeSampledBitmapFromUriは常にnullを返します。非常に長い研究の結果、BitmapFactory.decodeStreamに同じ入力ストリームを複数回使用することができないことが分かりました。 – Dika