2012-10-24 13 views
6

私はアンドロイドプロジェクトに画像を表示するためにダイアログボックスを使用しています。最初のものはうまく開きますが、私がそれを閉じてもう一度別のプロセスを表示すると、アプリケーションがメモリエラーで落ちます(それはsamsung galaxy s3上で実行されているので、問題ではありません)。写真のメモリが不足しています

エラー:

10-24 11:25:45.575: E/dalvikvm-heap(29194): Out of memory on a 31961104-byte allocation. 
10-24 11:25:45.580: E/AndroidRuntime(29194): FATAL EXCEPTION: main 
10-24 11:25:45.580: E/AndroidRuntime(29194): java.lang.OutOfMemoryError 
10-24 11:25:45.580: E/AndroidRuntime(29194): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:389) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:418) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at android.graphics.drawable.Drawable.createFromPath(Drawable.java:882) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at android.widget.ImageView.resolveUri(ImageView.java:569) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at android.widget.ImageView.setImageURI(ImageView.java:340) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at com.directenquiries.assessment.tool.AddAsset.loadPhoto(AddAsset.java:771) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at com.directenquiries.assessment.tool.AddAsset$11.onClick(AddAsset.java:748) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:936) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at android.widget.AdapterView.performItemClick(AdapterView.java:292) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at android.widget.AbsListView.performItemClick(AbsListView.java:1359) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2988) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at android.widget.AbsListView$1.run(AbsListView.java:3783) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at android.os.Handler.handleCallback(Handler.java:605) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at android.os.Handler.dispatchMessage(Handler.java:92) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at android.os.Looper.loop(Looper.java:137) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at android.app.ActivityThread.main(ActivityThread.java:4517) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at java.lang.reflect.Method.invokeNative(Native Method) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at java.lang.reflect.Method.invoke(Method.java:511) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760) 
10-24 11:25:45.580: E/AndroidRuntime(29194): at dalvik.system.NativeStart.main(Native Method) 

ロードコード:

public void loadPhotoList(){ 

    Cursor f = db.rawQuery("select * from stationphotos where StationObjectID = '"+ checkStationObjectID + "'", null); 
    final ArrayList<String> mHelperNames= new ArrayList<String>(); 

      if(f.getCount() != 0) { 
       f.moveToFirst(); 

       f.moveToFirst(); 
       while(!f.isAfterLast()) { 
        mHelperNames.add(f.getString(f.getColumnIndex("FilePath"))); 
        f.moveToNext(); 
       } 
      } 
    f.close(); 
    final String [] nameStrings = new String [mHelperNames.size()]; 

    for(int i=0; i<mHelperNames.size(); i++) 
     nameStrings[i] = mHelperNames.get(i).toString(); 


    AlertDialog.Builder builder = new AlertDialog.Builder(this); 

    builder.setTitle("Select Picture"); 
    builder.setItems(nameStrings, new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface dialog, int item) { 

      loadPhoto(mHelperNames.get(item).toString()); 

     } 

    }); 

    AlertDialog alert = builder.create(); 

    alert.show(); 

} 


public void loadPhoto(String imagepath){ 

    Dialog dialog = new Dialog(this); 
    dialog.setContentView(R.layout.activity_show_image); 
    dialog.setTitle("Image"); 
    dialog.setCancelable(true); 

    ImageView img = (ImageView) dialog.findViewById(R.id.imageView1); 
    img.setImageResource(R.drawable.ico_partial); 
    Uri imgUri = Uri.parse(imagepath); 
    img.setImageURI(imgUri); 


    dialog.show(); 
} 

編集:私はそれが今動作させるために使用しています何

public static Bitmap decodeSampledBitmapFromFile(String imagePath, int reqWidth, int reqHeight) { 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(imagePath, options); 

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

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeFile(imagePath, options); 
} 
    public 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) { 
     if (width > height) { 
      inSampleSize = Math.round((float)height/(float)reqHeight); 
     } else { 
      inSampleSize = Math.round((float)width/(float)reqWidth); 
     } 
    } 
    return inSampleSize; 
} 



img.setImageBitmap(decodeSampledBitmapFromFile(imagepath, 500, 500)); 
+0

?ヒープサイズ、ヒープフリーメモリとは何ですか? – sandrstar

+0

写真を読み込んだときにヒープショットが44MBに達しました。それを行う別の方法がありますか? 1 \t 45.258MB \t 44.830MB \t 438.242 KB 99.05%\t 57,128 – TMB87

+0

高さと幅が500であることをどのように知っていますか? –

答えて

5
Uri imgUri = Uri.parse(imagepath); 
    img.setImageURI(imgUri); 

Heap shot up to 44mb when I loaded the photo.

は、あなたがして大きな画像をロードする場合:210

これはおそらくあなたの問題です...私はこれを経験しているし、私の答えは、しかし、ベストプラクティスであるかどうかはわかりませんおそらくメモリが足りなくなるでしょう。また、一連のイメージをロードするときに適切なメモリ管理を使用しないと、メモリ不足になります。

ファイルをデコードするのにBitmapFactoryを使用し、BitmapFactory.Optionsを使用して、画面に表示された新しいスクラップビットマップを作成しました。

BitmapFactory.Optionsを使用すると、画像が消費する可能性のあるメモリをより詳細に制御できます。たとえば、解像度をサポートしていない画面でフルサイズの1920x1080イメージを表示する必要はありません。

特にあなたに役立つスタックオーバーフロー関連の質問がいくつかあります。Handling large Bitmapsは、BitmapFactory.OptionsオプションinJustDecodeBoundsに焦点を当てています。

最後に、この素​​晴らしい軽量ライブラリAqueryが見つかりました。それは、大きなファイルを扱うための方法の全体的な筏を持ち、それまではそれがうまくいきます。よく見て価値がある。 Web開発(したがって名前)に関してJqueryと同様のパラダイムを使用し、Viewsを操作するための短い構文を導入しています。フォールバック画像を含むローカルおよびリモートソースからの画像の読み込み、とのドキュメントのお得な情報の次のセクション:

メモリプロファイラ番組を何

http://code.google.com/p/android-query/wiki/ImageLoading

+0

これはおかげで、私はちょうどビットマップ工場を見て、今私は私は私が行くように圧縮している – TMB87