Android: Gallery picker and Camera intent crashes upon onActivityresult
<p>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.</p>

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

<p>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:</p>

<pre><code>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}: 
</code></pre>

<p>If I am using the camera intent or if I used the gallery picker this shows: </p>

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

<p>Below are the codes I used to start my camera intent:</p>

<pre><code>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);
}
</code></pre>

<p>And my codes to start gallery picker:</p>

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

<p>This is my codes to handle the results:</p>

<pre><code>protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    photo = null;
    if (requestCode == CAMERA_REQUEST &amp;&amp; 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 &amp;&amp; resultCode == RESULT_OK &amp;&amp; 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);
    }

}
</code></pre>

<p>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).</p>

<p>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: </p>

<pre><code>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) 
</code></pre>

<p>Also:</p>

<pre><code>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) 
</code></pre>

<p>More info, this is my createDialog function which actually causing problems.</p>

<pre><code>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();
</code></pre>

<p>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?</p>