santafebound santafebound - 9 months ago 140
Java Question

Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { }} to activity

I have a method that gives users the option to use the camera intent to capture a photo to then upload to the application:

public void UploadImageToFeed() {

CharSequence colors[] = new CharSequence[] {"Gallery", "Take a Picture"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Upload an Image");
builder.setIcon(R.drawable.ic_upload_image);
builder.setItems(colors, (dialog, which) -> {
if (which == 0) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
} else {
Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(), "camera.jpg");
Uri uri = Uri.fromFile(file);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(captureIntent, SELECT_PHOTO);
}
});
builder.show();

}


When a camera image is taken, I process the image to reduce its size before it is actually uploaded. This is where I get a crash.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

switch (requestCode) {
case SELECT_PHOTO:
if (resultCode == RESULT_OK) {

ImageView imageToUpload = (ImageView) findViewById(R.id.imageToUpload);
imageToUpload.setVisibility(View.VISIBLE);

Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
int nh = (int) (yourSelectedImage.getHeight() * (512.0 / yourSelectedImage.getWidth()));
Bitmap scaled = Bitmap.createScaledBitmap(yourSelectedImage, 512, nh, true);
imageToUpload.setImageBitmap(scaled);

}
}
}


Exception:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/-1/1/content://media/external/images/media/197149/ORIGINAL/NONE/724456777 flg=0x1 clip={text/uri-list U:content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F197149/ORIGINAL/NONE/724456777} }} to activity {com.yitter.android/com.yitter.android.activity.YeetActivity}: java.lang.IllegalArgumentException: width and height must be > 0


at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:829)
at android.graphics.Bitmap.createBitmap(Bitmap.java:808)
at android.graphics.Bitmap.createBitmap(Bitmap.java:739)
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:615)
at com.yitter.android.activity.YeetActivity$override.onActivityResult(YeetActivity.java:350)
at com.yitter.android.activity.YeetActivity$override.access$dispatch(YeetActivity.java)
at com.yitter.android.activity.YeetActivity.onActivityResult(YeetActivity.java:0)
at android.app.Activity.dispatchActivityResult(Activity.java:6456)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
at android.app.ActivityThread.-wrap16(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


 

My first suspicion was that the error occurs where I'm scaling the camera image down, but it's not so obvious from my stack trace. When I remove the two lines of code responsible for rescaling the image, I get the following error:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { }} to activity {com.yitter.android/com.yitter.android.activity.YeetActivity}: java.lang.NullPointerException: uri
at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: uri
at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:60)
at android.content.ContentResolver.openInputStream(ContentResolver.java:645)
at com.yitter.android.activity.YeetActivity.onActivityResult(YeetActivity.java:366)
at android.app.Activity.dispatchActivityResult(Activity.java:6456)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
at android.app.ActivityThread.-wrap16(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 


What could be going wrong?

Answer Source

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().