Skip to content

2. Picking Images

Kumar Bibek edited this page May 10, 2016 · 6 revisions

Picking images


To pick image/images

Use ImagePicker to pick and image or images.

It is mandatory to set a ImagePickerCallback before triggering the pickImage() method, or an exception will be raised.

ImagePicker imagePicker = new ImagePicker(Activity.this);
imagePicker.setImagePickerCallback(new ImagePickerCallback(){
        @Override
        public void onImagesChosen(List<ChosenImage> images) {
            // Display images
        }

        @Override
        public void onError(String message) {
            // Do error handling
        }
    }
);
// imagePicker.allowMultiple(); // Default is false
// imagePicker.shouldGenerateMetadata(false); // Default is true
// imagePicker.shouldGenerateThumbnails(false); // Default is true
imagePicker.pickImage();

After this call, you need to submit the onActivityResult(int requestCode, int resultCode, Intent data) to ImagePicker so that the processing might start.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == RESULT.OK) {
        if(requestCode == Picker.PICK_IMAGE_DEVICE) {
            imagePicker.submit(data);
        }
    }
}

The ImagePickerCallback will be triggered once the processing is done.

The one thing you have to handle is when your Activity is killed when the user is still choosing a photo. In such a scenario, ImagePicker reference will be destroyed since the Activity will be re-created. An additional check is required to handle this scenario. You just have to create a new ImagePicker object, attach the callback and call the ImagePicker.submit(data) method.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == RESULT.OK) {
        if(requestCode == Picker.PICK_IMAGE_DEVICE) {
            if(imagePicker == null) {
                 imagePicker = new ImagePicker(Activity.this);
                 imagePicker.setImagePickerCallback(imagePickerCallback);
            }
            imagePicker.submit(data);
        }
    }
}
Capture a photo using camera

Use CameraImagePicker to get a photo captured using the device's camera.

It is mandatory to set a ImagePickerCallback before triggering the pickImage() method, or an exception will be raised. While most of the code is similar to the ImagePicker, there's one important difference between the two.

CameraImagePicker imagePicker = new CameraImagePicker(Activity.this);
imagePicker.setImagePickerCallback(new ImagePickerCallback(){
        @Override
        public void onImagesChosen(List<ChosenImage> images) {
            // Display images
        }

        @Override
        public void onError(String message) {
            // Do error handling
        }
    }
);
// imagePicker.shouldGenerateMetadata(false); // Default is true
// imagePicker.shouldGenerateThumbnails(false); // Default is true
String outputPath = imagePicker.pickImage();

Notice, String outputPath = imagePicker.pickImage(); method. The return value is a String, which is where the captured image from the camera is going to be saved. You can save this string variable during the onSaveInstanceState callback of the Activity.

@Override
    protected void onSaveInstanceState(Bundle outState) {
        // You have to save path in case your activity is killed.
        // In such a scenario, you will need to re-initialize the CameraImagePicker
        outState.putString("picker_path", outputPath);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // After Activity recreate, you need to re-initialize these
        // two values to be able to re-initialize CameraImagePicker
        if (savedInstanceState != null) {
            if (savedInstanceState.containsKey("picker_path")) {
                outputPath = savedInstanceState.getString("picker_path");
            }
        }
        super.onRestoreInstanceState(savedInstanceState);
    }

After this call, you need to submit the onActivityResult(int requestCode, int resultCode, Intent data) to CameraImagePicker so that the processing might start.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == RESULT.OK) {
        if(requestCode == Picker.PICK_IMAGE_CAMERA) {
            imagePicker.submit(data);
        }
    }
}

The ImagePickerCallback will be triggered once the processing is done.

The one thing you have to handle is when your Activity is killed when the user is still choosing a photo. In such a scenario, CameraImagePicker reference will be destroyed since the Activity will be re-created. An additional check is required to handle this scenario. You just have to create a new CameraImagePicker object, re-initialize it, attach the callback and call the CameraImagePicker.submit(data) method.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == RESULT.OK) {
        if(requestCode == Picker.Picker.PICK_IMAGE_CAMERA) {
            if(imagePicker == null) {
                 imagePicker = new CameraImagePicker(Activity.this);
                 imagePicker.reinitialize(outputPath);
                 // OR in one statement
                 // imagePicker = new CameraImagePicker(Activity.this, outputPath);
                 imagePicker.setImagePickerCallback(imagePickerCallback);
            }
            imagePicker.submit(data);
        }
    }
}
Clone this wiki locally