有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

显示照片的java内存不足

我在安卓项目中使用一个对话框来显示图像。第一个可以正常打开,但当我关闭它并再次执行该过程以显示另一个时,应用程序会出现内存错误(它运行在三星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 安卓.graphics.BitmapFactory.nativeDecodeStream(Native Method)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at 安卓.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at 安卓.graphics.BitmapFactory.decodeFile(BitmapFactory.java:389)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at 安卓.graphics.BitmapFactory.decodeFile(BitmapFactory.java:418)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at 安卓.graphics.drawable.Drawable.createFromPath(Drawable.java:882)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at 安卓.widget.ImageView.resolveUri(ImageView.java:569)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at 安卓.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.安卓.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:936)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at 安卓.widget.AdapterView.performItemClick(AdapterView.java:292)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at 安卓.widget.AbsListView.performItemClick(AbsListView.java:1359)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at 安卓.widget.AbsListView$PerformClick.run(AbsListView.java:2988)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at 安卓.widget.AbsListView$1.run(AbsListView.java:3783)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at 安卓.os.Handler.handleCallback(Handler.java:605)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at 安卓.os.Handler.dispatchMessage(Handler.java:92)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at 安卓.os.Looper.loop(Looper.java:137)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at com.安卓.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));

共 (1) 个答案

  1. # 1 楼答案

    我经历过这种情况,我不知道我的答案是否是最佳实践,但是

    这可能是您的问题:

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

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

    如果你加载了一个大的图像,那么你可能会耗尽内存。此外,如果在加载一系列图像时未能使用适当的内存管理,您将耗尽内存

    我使用BitmapFactory对文件进行解码,并使用BitmapFactory.Options创建一个新的sclaed位图,然后显示在屏幕上

    这个想法是BitmapFactory。选项使您能够更好地控制图像可能使用的潜在内存。例如,不需要在甚至不支持这些分辨率的屏幕上以全尺寸显示1920x1080图像

    有一些相关的stackoverflow问题可能会对您有所帮助,特别是:Handling large Bitmaps,它主要关注BitmapFactory。选项inJustDecodeBounds

    最后,我找到了这个名为Aquery的伟大的轻量级库。它有一整套处理大型文件的方法,到目前为止,它做得很好。很值得一看。在web开发方面,它使用了与Jquery类似的范例(因此得名),并引入了一种用于操作视图的较短语法。他们文档的以下部分涉及本地和远程源的图像加载,包括回退图像:

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