Search code examples
javaandroidandroid-intentbitmaponactivityresult

Setting bitmap via onActivityResult: Failure delivering result


I'm using startActivityForResult and onActivityResult in an attempt to get images from the camera / gallery and display them in programatically generated ImageViews. I have the Gallery images working and displaying, however I'm facing a problem with the Camera images.

I call the startActivityForResult in my listAdapter for both the gallery button (opens a cusotm gallery class to select multiple images) and the camera button (opens camera app) in formListAdapter.java:

private void openCustomGallery() {
    Intent intent = new Intent(context, CustomPhotoGalleryActivity.class);
    context.startActivityForResult(intent, PICK_IMAGE_MULTIPLE);
}

private void launchCamera() {
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    context.startActivityForResult(cameraIntent, CAMERA_REQUEST);
}

and then call the onActivityResult using a switch statement to determine the request codes from my main activity formsActivity.java:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case PICK_IMAGE_MULTIPLE:
                Log.i("Gallery Request Fired", "Yes");

                //Declare imagePaths / ArrayList to store bitmaps
                imagesPathList = new ArrayList<String>();
                bitmapImages = new ArrayList<Bitmap>();
                String[] imagesPath = data.getStringExtra("data").split("\\|");

                //Loop through selected images
                for (int i = 0; i < imagesPath.length; i++) {
                    //Reduce file size
                    compressBitmap = decodeSampledBitmapFromResource(imagesPath[i],
                            350, 350);
                    //Add image to array
                    bitmapImages.add(compressBitmap);
                }

                //Send bitmap to listview & refresh
                formListAdapter.getBitmap(bitmapImages);
                adapter.notifyDataSetChanged();
                break;
            case CAMERA_REQUEST:
                Log.i("Camera Request Fired", "Yes");

                //I've tried various logic here, however bitmap is always null and never set.
                Bitmap datifoto = null;
                Uri picUri = data.getData();//<- get Uri here from data intent
                if (picUri != null) {
                    try {
                        //Not setting datifoto
                        datifoto = android.provider.MediaStore.Images.Media.getBitmap(
                                this.getContentResolver(),
                                picUri);

                        //ERROR HERE ==> Null pointer exception because "datifoto" is null and not set
                        bitmapImages.add(datifoto);
                    } catch (FileNotFoundException e) {
                        throw new RuntimeException(e);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
                break;
            default:
                Log.i("Default", "Yes");
                break;
        }
    }
}

However, this always a returns an " Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference" because i'm attempting to add a null object to the array, due to the bitmap not being set:

Caused by : java.lang.RuntimeException : Failure delivering result ResultInfo {
    who = null,
    request = 1888,
    result = -1,
    data = Intent {act = inline - data dat = content : //media/external/images/media/18125 (has extras) }} 
    to activity {net.swnton_blandpjkeary.james.pjkearymobileinspections/net.swnton_blandpjkeary.james.pjkearymobileinspections.formsActivity}: 
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference   

Ideally, I would be setting the bitmap, adding it to the ArrayList and then passing this list to the adapter for it to be set in the ImageView. I'm assuming i'm not handling the Intent data correctly, but I've tried numerous solutions all with the same outcome.

Thanks in advance for any help :)


Solution

  • I had to declare the bitmapImages = new ArrayList<>(); outside of the switch statement... :|