2014-02-24 35 views
0

我試圖在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) 
+0

http://stackoverflow.com/questions/2928002/outofmemoryerror-bitmap-size-exceeds-vm-budget-android看到這個位圖加載沒有內存異常 –

回答

1

使用這個..

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(); 
    } 
+0

非常感謝,但它再次正常工作,並在另一次又一次作品相同的錯誤崩潰,等等... –

+0

也許我應該在onDestroy()中做什麼? –

+0

沒有什麼............我認爲 – Piyush