简体   繁体   中英

Android: Gallery picker and Camera intent crashes upon onActivityresult

So I have this page in my app which lets user choose to either take a picture with their camera or pick an image from gallery.

Upon choosing an alert dialog with the chosen image set as icon will be shown confirming.

Now here comes my problem, whenever I try to get an image, using either the gallery picker or camera intent, my app crashes right after with error:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=null} to activity {my.com.thelorry.ken.thelorrypartner/com.example.Activity}: 

If I am using the camera intent or if I used the gallery picker this shows:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=null} to activity {com.example/com.example.Activity}: 

Below are the codes I used to start my camera intent:

public void startCamera(){
    ContentValues values;
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, "New Picture");
    values.put(MediaStore.Images.Media.DESCRIPTION, "From phone Camera");
    imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    startActivityForResult(cameraIntent, CAMERA_REQUEST);
}

And my codes to start gallery picker:

public void pickGallery(){
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, GALLERY_REQUEST);
}

This is my codes to handle the results:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    photo = null;
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK ) {
        try {
            photo = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
            photo = getResizedBitmap(photo, 600, 450);
            File file = new File(imageurl);
            file.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
        createDialog(photo);
    }if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK && data != null) {
        Uri pickedImage = data.getData();
        String[] filePath = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
        cursor.moveToFirst();
        String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
        photo = getResizedBitmap(bitmap, 600, 450);
        cursor.close();
        createDialog(photo);
    }

}

Also, odd enough, this codes seems to crash only on Samsung phones based on my crash reports, and only on some models such as the Galaxy J7, Note5, Note4 (so far, app doesn't have a huge userbase yet).

Edit: After actually getting a phone that could reproduce the crash I realize that codes above might not be the problem after all, this is the what I have in logcats:

Process: com.example, PID: 6220
                                                                               java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=null} to activity {com.example/com.example.Activity}: android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
                                                                                   at android.app.ActivityThread.deliverResults(ActivityThread.java:4019)
                                                                                   at android.app.ActivityThread.handleSendResult(ActivityThread.java:4062)
                                                                                   at android.app.ActivityThread.access$1400(ActivityThread.java:177)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:135)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5930)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
                                                                                Caused by: android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
                                                                                   at android.content.res.Resources.getValue(Resources.java:1542)
                                                                                   at android.support.v7.widget.ResourcesWrapper.getValue(ResourcesWrapper.java:204)
                                                                                   at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:321)
                                                                                   at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:197)
                                                                                   at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:192)
                                                                                   at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:66)
                                                                                   at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:71)
                                                                                   at com.android.internal.app.AlertController.setupTitle(AlertController.java:565)
                                                                                   at com.android.internal.app.AlertController.setupView(AlertController.java:474)
                                                                                   at com.android.internal.app.AlertController.installContent(AlertController.java:240)
                                                                                   at android.app.AlertDialog.onCreate(AlertDialog.java:356)
                                                                                   at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
                                                                                   at android.app.Dialog.show(Dialog.java:274)
                                                                                   at com.example.Activity.createDialog(Activity.java:697)
                                                                                   at com.example.Activity.onActivityResult(Activity.java:618)
                                                                                   at android.app.Activity.dispatchActivityResult(Activity.java:6441)
                                                                                   at android.app.ActivityThread.deliverResults(ActivityThread.java:4015)
                                                                                   at android.app.ActivityThread.handleSendResult(ActivityThread.java:4062) 
                                                                                   at android.app.ActivityThread.access$1400(ActivityThread.java:177) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:135) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5930) 
                                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 

Also:

FATAL EXCEPTION: main
                                                                               Process: com.example, PID: 9794
                                                                               java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1889, result=-1, data=Intent { dat=content://media/external/images/media/3196 flg=0x1 (has extras) }} to activity {com.example/com.example.Activity}: android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
                                                                                   at android.app.ActivityThread.deliverResults(ActivityThread.java:4019)
                                                                                   at android.app.ActivityThread.handleSendResult(ActivityThread.java:4062)
                                                                                   at android.app.ActivityThread.access$1400(ActivityThread.java:177)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:135)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5930)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
                                                                                Caused by: android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
                                                                                   at android.content.res.Resources.getValue(Resources.java:1542)
                                                                                   at android.support.v7.widget.ResourcesWrapper.getValue(ResourcesWrapper.java:204)
                                                                                   at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:321)
                                                                                   at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:197)
                                                                                   at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:192)
                                                                                   at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:66)
                                                                                   at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:71)
                                                                                   at com.android.internal.app.AlertController.setupTitle(AlertController.java:565)
                                                                                   at com.android.internal.app.AlertController.setupView(AlertController.java:474)
                                                                                   at com.android.internal.app.AlertController.installContent(AlertController.java:240)
                                                                                   at android.app.AlertDialog.onCreate(AlertDialog.java:356)
                                                                                   at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
                                                                                   at android.app.Dialog.show(Dialog.java:274)
                                                                                   at com.example.Activity.createDialog(Activity.java:697)
                                                                                   at com.example.Activity.onActivityResult(Activity.java:639)
                                                                                   at android.app.Activity.dispatchActivityResult(Activity.java:6441)
                                                                                   at android.app.ActivityThread.deliverResults(ActivityThread.java:4015)
                                                                                   at android.app.ActivityThread.handleSendResult(ActivityThread.java:4062) 
                                                                                   at android.app.ActivityThread.access$1400(ActivityThread.java:177) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:135) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5930) 
                                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 

More info, this is my createDialog function which actually causing problems.

Drawable icon = new BitmapDrawable(this.getResources(),photo);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getString(R.string.business_post_camera_dialog_title))
                .setMessage(getString(R.string.business_post_camera_dialog_message))
                .setCancelable(false)
                .setPositiveButton(R.string.sign_confirm_btn, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        doSomething();
                    }
                })
                .setNegativeButton(R.string.sign_cancel_btn, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // do nothing
                    }
                })
                .setIcon(-1).setIcon(icon); //setIcon(-1) as a workaround for bug in Android Lollipop 5.0
        AlertDialog alert = builder.create();
        alert.show();

Apparently .setIcon(-1) causes the crash on Samsung phones, but without it I wouldn't be able to display my images there on certain phones still on Lollipop 5.0, any solution?

The problem is here:

...
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
...

You are trying to use setImageResource(int id) with a non-exist image. You can use it like this:

imageView.setImageResource(R.drawable.image_name)

The same thing is available for TextView too.

textView.setText("" + intValue);

Try to use below method for dialog,

    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    Drawable icon = new BitmapDrawable(this.getResources(),photo);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(getString(R.string.business_post_camera_dialog_title))
            .setMessage(getString(R.string.business_post_camera_dialog_message))
            .setCancelable(false)
            .setPositiveButton(R.string.sign_confirm_btn, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    doSomething();
                }
            })
            .setNegativeButton(R.string.sign_cancel_btn, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // do nothing
                }
            });


         if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){

          builder.setIcon(-1).setIcon(icon);

         } else{
                 builder.setIcon(icon);
        }
    AlertDialog alert = builder.create();
    alert.show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM