我試圖在sharedpreferences
中保存位圖並在啓動時重新恢復。 但它一次正常工作,如果我關閉並重新啓動應用程序,它會給我「強制關閉」錯誤,然後如果我再次運行它可以正常工作並再次崩潰等。將位圖保存在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)
http://stackoverflow.com/questions/2928002/outofmemoryerror-bitmap-size-exceeds-vm-budget-android看到這個位圖加載沒有內存異常 –