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;
}
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=0, data=null} to activity
Adding this first conditional should work:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode != RESULT_CANCELED){
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent
try to change this
if (resultCode == RESULT_OK) {
to
if (resultCode == RESULT_OK && data.getData()!=null) {
Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { }} to activity
ACTION_IMAGE_CAPTURE
does not return a Uri
. You know the Uri
where the image is supposed to be — you put it in EXTRA_OUTPUT
. You need to hold onto that location (including via the saved instance state Bundle
) and look there.
Also note that Uri.fromFile()
will not work on Android 7.0+, once you raise your targetSdkVersion
to 25 or higher.
This sample app demonstrates using ACTION_IMAGE_CAPTURE
, including using FileProvider
to avoid Uri.fromFile()
.
Android camera error Failure delivering result ResultInfo
This is my code to make the camera works. You may try this.
private void activeTakePhoto() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_IMAGE_CAPTURE:
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
//to generate random file name
String fileName = "tempimg.jpg";
try {
photo = (Bitmap) data.getExtras().get("data");
//captured image set in imageview
imageView.setImageBitmap(photo);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Related Topics
I Cant See Any Firebaserecycleradapter Items on My Layout
Android Intent for Sending Email with Attachment
How to Check If Class Exists Somewhere in Package
When Onbindviewholder Is Called and How It Works
How to Override Action Bar Back Button in Android
Using Serviceloader on Android
Values of Counter Changes After Scrolling Expendablelistview
Save JSON Outputted from a Url to a File
Spark Error - Unsupported Class File Major Version
"Invalid Privatekey" When Using Jsch
Google Maps API and Custom Polyline Route Between Markers
How to Move/Rename File from Internal App Storage to External Storage on Android
How to Change Method Behaviour Through Reflection
Android Drawable Speech Bubble
Convert String to Date in Java
Gson Expected Begin_Array But Was Begin_Object
Google Cloud Messaging: Don't Receive Alerts When iOS App Is in Background
Fixing 'Path' in Environment Variables in Windows 7 for Multiple Applications