Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正在SharedReferences中保存位图,内存不足_Java_Android_Eclipse - Fatal编程技术网

Java 正在SharedReferences中保存位图,内存不足

Java 正在SharedReferences中保存位图,内存不足,java,android,eclipse,Java,Android,Eclipse,我正在尝试将位图保存在SharedReferences中,并在启动时再次恢复它。 但有一次它工作正常,如果我关闭并重新启动应用程序,它会给我“强制关闭”错误,然后如果我再次运行它,它工作正常,然后再次崩溃等等 有什么问题 我的onActivityResult代码如下: @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generate

我正在尝试将位图保存在
SharedReferences
中,并在启动时再次恢复它。 但有一次它工作正常,如果我关闭并重新启动应用程序,它会给我“强制关闭”错误,然后如果我再次运行它,它工作正常,然后再次崩溃等等

有什么问题

我的
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();
    }
公共无效解码文件(字符串文件路径){
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码文件(文件路径,o);
//我们要扩展到的新尺寸
所需最终整型尺寸=2048;
//找到正确的刻度值。它应该是2的幂。
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=2;
while(true){
如果(宽度\u tmp<要求的\u尺寸和高度\u tmp<要求的\u尺寸)
打破
宽度_tmp/=2;
高度_tmp/=2;
比例*=2;
}
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
bmp=BitmapFactory.decodeFile(文件路径,o2);
}
解码文件(文件路径);//在这里调用函数
img_logo.setImageBitmap(bmp);
设置=GetSharedReferences(“pref”,0);
SharedReferences.Editor prefsEditor=settings.edit();
prefsEditor.putString(“photo1”,文件路径);
提交();
}

在加载位图时看到这一点,没有内存例外情况下加载了很多次,但它一次工作正常,一次又一次地出现相同的错误并崩溃,如此等等…也许我应该在onDestroy()中做些什么?没有什么………我想给你我的整个应用程序文件夹,让你看看?在onDestroy()中,我称之为“this.finish();”和“System.exit(0);”,现在它工作正常。可以吗?