Note that there are some explanatory texts on larger screens.

plurals
  1. POBitmap URI stays the same Android Out Of Memory Exception
    text
    copied!<p>I have a question i'm taking an image and uploading it to the server. For the first time it all works great. But the 2nd time i'm taking a picture i'm getting an Out Of Memory Exception at this line:</p> <pre><code>_mPhoto = android.provider.MediaStore.Images.Media .getBitmap(cr, _mUri); </code></pre> <p>If i'm debugging _mUri it's the same for the 1st one and the 2nd time. I think this shouldnt..</p> <p>I'm getting this error: <a href="http://pastebin.com/uVduy3d9" rel="nofollow">http://pastebin.com/uVduy3d9</a></p> <p>These are my 3 methods used to take a picture:</p> <p>First check if phone has a camera:</p> <pre><code>Camera cam = Camera.open(); if (cam != null) { if (savedInstanceState != null &amp;&amp; savedInstanceState.getBoolean("Layout")) { setContentView(R.layout.registration_edit); initializeAccountDetails((User) savedInstanceState .getSerializable(EXTRA_MESSAGE)); inAccountDetails = true; } else { setContentView(R.layout.step_4); ((Button) findViewById(R.id.snap)).setOnClickListener(this); ((Button) findViewById(R.id.rotate)).setOnClickListener(this); cam.stopPreview(); cam.release(); cam = null; } } else { if (savedInstanceState != null &amp;&amp; savedInstanceState.getBoolean("Layout")) { setContentView(R.layout.registration_edit); initializeAccountDetails((User) savedInstanceState .getSerializable(EXTRA_MESSAGE)); inAccountDetails = true; } else { setContentView(R.layout.step_4b); } } </code></pre> <p>When clicking on the button Snap the following onClick event is fired:</p> <pre><code>@Override public void onClick(View v) { if (v.getId() == R.id.snap) { File directory = new File(Environment.getExternalStorageDirectory() + "/BandenAnalyse/Images/"); if (directory.exists()) { Intent i = new Intent("android.media.action.IMAGE_CAPTURE"); File f = new File(Environment.getExternalStorageDirectory(), "/BandenAnalyse/Images/IMG_" + _timeStamp + ".jpg"); _mUri = Uri.fromFile(f); i.putExtra(MediaStore.EXTRA_OUTPUT, _mUri); startActivityForResult(i, TAKE_PICTURE); } else { directory.mkdir(); this.onClick(v); } } else { if (_mPhoto != null) { Matrix matrix = new Matrix(); matrix.postRotate(90); _mPhoto = Bitmap.createBitmap(_mPhoto, 0, 0, _mPhoto.getWidth(), _mPhoto.getHeight(), matrix, true); ((ImageView) findViewById(R.id.photo_holder)) .setImageBitmap(_mPhoto); } } } </code></pre> <p>When the picture is taken the result method will be fired:</p> <pre><code>@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case TAKE_PICTURE: if (resultCode == Activity.RESULT_OK) { getContentResolver().notifyChange(_mUri, null); ContentResolver cr = getContentResolver(); try { _mPhoto = android.provider.MediaStore.Images.Media .getBitmap(cr, _mUri); Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; int scale = _mPhoto.getWidth() / width; BitmapFactory.Options o = new BitmapFactory.Options(); o.inSampleSize = 8; Debug.out(PATH_TO_PHOTO); Bitmap temp = BitmapFactory.decodeFile(PATH_TO_PHOTO, o); _mPhoto = Bitmap.createScaledBitmap( temp, _mPhoto.getWidth() / scale, _mPhoto.getHeight() / scale, false); temp.recycle(); ((ImageView) findViewById(R.id.photo_holder)) .setImageBitmap(_mPhoto); } catch (Exception e) { Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT) .show(); } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload