0

I am trying to save bitmap in sharedpreferences and restore it again in start-up. But it is working fine one time and if I close and restart the app it gives me "force close" error, and then if I run it again it works fine and again crashes and so on.

What is the problem?

My onActivityResult code is here:

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


        }



}


}

in Log.d I get a lot of Red lines. but I think this is the problem:

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)
Cœur
  • 37,241
  • 25
  • 195
  • 267
SaDeGH_F
  • 471
  • 1
  • 3
  • 14
  • http://stackoverflow.com/questions/2928002/outofmemoryerror-bitmap-size-exceeds-vm-budget-android see this for bitmap loading without memory exception – Adhikari Bishwash Feb 24 '14 at 08:01

1 Answers1

1

Use this..

  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();
    }
Piyush
  • 18,895
  • 5
  • 32
  • 63