So I am aware of the camera's issue on Samsung devices. You need to create a Uri before calling the camera intent like so:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.TITLE, m_username); mImageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri); startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
This example creates a Uri for the camera to store the data in. My app has a camera function in two locations. Both are identical (direct copy paste), but in one activity the mImageUri
object becomes null during the camera process, while in the other it doesn't. The latter works fine, but the former gives a null value which in turn stops me from saving the image, previewing it, etc. I will provide the onResult code below:
case CAMERA_PIC_REQUEST : if(resultCode == RESULT_OK) { if(intent != null) { //For non-Samsung devices Log.e("Intent value:", intent.toString()); mImageUri = intent.getData(); } mAvatar = BitmapStatic.createImage(mImageUri, this); Drawable draw = new BitmapDrawable(getResources(), mAvatar); m_photoButtonE.setImageDrawable(draw); m_imageChanged = true; } break;
The above example is from the working activity. Below is the activity where this doesn't work:
//On pressing the "Take Photo" button: Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.TITLE, mUsername); mImageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); if(mImageUri == null) { Log.e("image uri is null", "what?"); } else { Log.e("oh nevermind", "image uri is NOT null"); } cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri); startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); //On Result: case CAMERA_PIC_REQUEST : if(resultCode == RESULT_OK) { if(intent != null) { Log.e("Intent value:", intent.toString()); mImageUri = intent.getData(); } else { Log.e("Intent is null", "yep it is."); if(mImageUri == null) { Log.e("nullcheck on memberimageuri", "its null"); } else { Log.e("nullcheckon memberimage", mImageUri.toString()); } } mImage = BitmapStatic.createImage(mImageUri, this); Drawable draw = new BitmapDrawable(getResources(), mImage); mPhotoPreview.setImageDrawable(draw); } break;
Notice the log files I have put in this 2nd example. When I click the "Take Picture" button, the null check for the mImageUri
object returns true. So this object is not null. But somewhere during the picture taking process that value DOES become null. The 2nd check during onResult() returns false. Again, this is almost identical to my setup in the other activity, where it works fine.
Any thoughts?
Your activity gets destroyed during the Camera activity operation and re-created afterwards. You should use onSaveInstanceState/onRestoreInstanceState mechanism in your activity to preserve the image URI (and any other data) upon the activity restarts.
Like this:
@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (mImageUri != null) { outState.putString("cameraImageUri", mImageUri.toString()); } } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); if (savedInstanceState.containsKey("cameraImageUri")) { mImageUri = Uri.parse(savedInstanceState.getString("cameraImageUri")); } }
I can suggest to create the initial (temporary) file at first, then pass the Uri in the Intent. In the function "onActivityResult()" you will have the file with the filled image.
File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "folderName"); dir.mkdirs(); if (dir.exists()) { try { imageFile = File.createTempFile("IMG_", ".jpg", dir); } catch (IOException e) { e.printStackTrace(); } } if (imageFile != null) { Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile)); activity.startActivityForResult(cameraIntent, REQUEST_CODE); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With