android camera : Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity
Make sure you have both Camera Permission and READ/WRITE External Storage Permissions.
Try this is working like charm with me
private String selectedImagePath = "";
final private int PICK_IMAGE = 1;
final private int CAPTURE_IMAGE = 2;
public Uri setImageUri() {
// Store image in dcim
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png");
Uri imgUri = Uri.fromFile(file);
this.imgPath = file.getAbsolutePath();
return imgUri;
}
public String getImagePath() {
return imgPath;
}
btnGallery.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);
}
});
btnCapture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
startActivityForResult(intent, CAPTURE_IMAGE);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_CANCELED) {
if (requestCode == PICK_IMAGE) {
selectedImagePath = getAbsolutePath(data.getData());
imgUser.setImageBitmap(decodeFile(selectedImagePath));
} else if (requestCode == CAPTURE_IMAGE) {
selectedImagePath = getImagePath();
imgUser.setImageBitmap(decodeFile(selectedImagePath));
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
public Bitmap decodeFile(String path) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeFile(path, o2);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
public String getAbsolutePath(Uri uri) {
String[] projection = { MediaColumns.DATA };
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}
Try this, Yesterday i had same problem. using below code i rectify problem. just try on it.
Note : And also add configChanges
for particular activity. while calling the camera intent activity will be refreshed. you need to avoid that. android:configChanges="orientation|keyboardHidden|screenSize"
btnGallery.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 1);
}
});
btnCapture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment
.getExternalStorageDirectory(),
"test.jpg");
outputFileUri = Uri.fromFile(file);
Log.d("TAG", "outputFileUri intent"
+ outputFileUri);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
outputFileUri);
startActivityForResult(intent, 0);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 0:
if (resultCode == RESULT_OK) {
Log.d("TAG", "outputFileUri RESULT_OK" + outputFileUri);
if (outputFileUri != null) {
Bitmap bitmap;
bitmap = decodeSampledBitmapFromUri(outputFileUri,
image.getWidth(), image.getHeight());
if (bitmap == null) {
Toast.makeText(getApplicationContext(),
"the image data could not be decoded",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(
getApplicationContext(),
"Decoded Bitmap: " + bitmap.getWidth() + " x "
+ bitmap.getHeight(), Toast.LENGTH_LONG)
.show();
image.setImageBitmap(bitmap);
}
}
}
break;
case 1:
if (resultCode == RESULT_OK) {
Uri targetUri = data.getData();
Log.d("TAG", "datae" + targetUri);
Bitmap bitmap;
bitmap = decodeSampledBitmapFromUri(targetUri,
image.getWidth(), image.getHeight());
if (bitmap == null) {
Toast.makeText(getApplicationContext(),
"the image data could not be decoded",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(
getApplicationContext(),
"Decoded Bitmap: " + bitmap.getWidth() + " x "
+ bitmap.getHeight(), Toast.LENGTH_LONG)
.show();
image.setImageBitmap(bitmap);
}
}
break;
default:
break;
}
}
public Bitmap decodeSampledBitmapFromUri(Uri uri, int reqWidth,
int reqHeight) {
Bitmap bm = null;
try {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver()
.openInputStream(uri), null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(uri), null, options);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_LONG).show();
}
return bm;
}
public int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height / (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
}
return inSampleSize;
}
I think following line causes the issue.
Bitmap photo = (Bitmap) data.getExtras().get("data");
You pass the file uri already as following
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoUri);
When you pass EXTRA_OUTPUT with a URI, the intent you receive in onActivityResult() will be null. If you want to get image in this method, fetch image with the path you pass with EXTRA_OUTPUT.