This question already has answers here:
Strange out of memory issue while loading an image to a Bitmap object

(43个答案)


6年前关闭。





我试图将位图保存在sharedpreferences中,并在启动时再次将其还原。
但是它一次运行正常,如果我关闭并重新启动应用程序,它会给我“强制关闭”错误,然后如果我再次运行它,它将运行正常,然后再次崩溃,依此类推。

问题是什么?

我的onActivityResult代码在这里:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == GALLERY_PICTURE) {
        if (resultCode == RESULT_OK) {


                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                if (cursor != null) {
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();


               Bitmap photo_gallery = BitmapFactory.decodeFile(filePath);
               img_logo.setImageBitmap(photo_gallery);
               settings = getSharedPreferences("pref", 0);
               SharedPreferences.Editor prefsEditor = settings.edit();
                    prefsEditor.putString("photo1", filePath);
                    prefsEditor.commit();
                }


            } else {
                Toast.makeText(getApplicationContext(), "Cancelled",
                        Toast.LENGTH_SHORT).show();
            }
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(getApplicationContext(), "Cancelled",
                    Toast.LENGTH_SHORT).show();
        }
     else if (requestCode == CAMERA_REQUEST) {
        if (resultCode == RESULT_OK) {
            String[] projection = { MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(mCapturedImageURI, projection, null, null, null);
     int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            String capturedImageFilePath = cursor.getString(column_index_data);
            Log.d("photos*******"," in camera take int  "+capturedImageFilePath);

            Bitmap photo_camera = BitmapFactory.decodeFile(capturedImageFilePath);

            if(data != null)
            {
                img_logo.setImageBitmap(photo_camera);
                settings = getSharedPreferences("pref", 0);
                SharedPreferences.Editor prefsEditor = settings.edit();
                    prefsEditor.putString("photo1", capturedImageFilePath);
                    prefsEditor.commit();
            }


        }



}


}


在Log.d中,我得到了很多红线。但我认为这是问题所在:

02-24 11:12:07.026: E/AndroidRuntime(29791): FATAL EXCEPTION: main
02-24 11:12:07.026: E/AndroidRuntime(29791): java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=7235KB, Allocated=2862KB, Bitmap Size=8748KB)

最佳答案

用这个..

  public void decodeFile(String filePath) {

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 2048;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;

    int scale = 2;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    bmp = BitmapFactory.decodeFile(filePath, o2);


}

    decodeFile(filePath);// Call Function here
    img_logo.setImageBitmap(bmp);
    settings = getSharedPreferences("pref", 0);
    SharedPreferences.Editor prefsEditor = settings.edit();
    prefsEditor.putString("photo1", filePath);
    prefsEditor.commit();
    }

关于java - 将位图保存在共享首选项中,内存不足,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21981928/

10-10 02:37