private void processResultFromCamera()

in src/android/CameraLauncher.java [486:629]


    private void processResultFromCamera(int destType, Intent intent) throws IOException {
        int rotate = 0;

        // Create an ExifHelper to save the exif data that is lost during compression
        ExifHelper exif = new ExifHelper();

        InputStream input = null;
        String mimeType;
        if (this.allowEdit && this.croppedUri != null) {
            input = new FileInputStream(this.croppedFilePath);
            mimeType = FileHelper.getMimeTypeForExtension(this.croppedFilePath);
        }
        else {
            input = cordova.getActivity().getContentResolver().openInputStream(imageUri);
            mimeType = FileHelper.getMimeType(imageUri.toString(), cordova);
        }

        if (input == null) {
            throw new IOException("Unable to open result source.");
        }

        byte[] sourceData = readData(input);

        try {
            if (this.encodingType == JPEG) {
                try {
                    //We don't support PNG, so let's not pretend we do
                    exif.createInFile(new ByteArrayInputStream(sourceData));
                    exif.readExifData();
                    rotate = exif.getOrientation();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            Bitmap bitmap = null;
            Uri galleryUri = null;

            // CB-5479 When this option is given the unchanged image should be saved
            // in the gallery and the modified image is saved in the temporary
            // directory
            if (this.saveToPhotoAlbum) {
                GalleryPathVO galleryPathVO = getPicturesPath();
                galleryUri = Uri.fromFile(new File(galleryPathVO.getGalleryPath()));

                if (this.allowEdit && this.croppedUri != null) {
                    writeUncompressedImage(croppedUri, galleryUri);
                } else {
                    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
                        writeTakenPictureToGalleryLowerThanAndroidQ(galleryUri);
                    } else { // Android Q or higher
                        writeTakenPictureToGalleryStartingFromAndroidQ(galleryPathVO);
                    }
                }
            }

            // If sending base64 image back
            if (destType == DATA_URL) {
                bitmap = getScaledAndRotatedBitmap(sourceData, mimeType);

                if (bitmap == null) {
                    // Try to get the bitmap from intent.
                    bitmap = (Bitmap) intent.getExtras().get("data");
                }

                // Double-check the bitmap.
                if (bitmap == null) {
                    LOG.d(LOG_TAG, "I either have an unreadable imageUri or null bitmap");
                    this.failPicture("Unable to create bitmap!");
                    return;
                }

                this.processPicture(bitmap, this.encodingType);
            }

            // If sending filename back
            else if (destType == FILE_URI) {
                // If all this is true we shouldn't compress the image.
                if (this.targetHeight == -1 && this.targetWidth == -1 && this.mQuality == 100 &&
                    !this.correctOrientation) {

                    // If we saved the uncompressed photo to the album, we can just
                    // return the URI we already created
                    if (this.saveToPhotoAlbum) {
                        this.callbackContext.success(galleryUri.toString());
                    } else {
                        Uri uri = Uri.fromFile(createCaptureFile(this.encodingType, System.currentTimeMillis() + ""));

                        if (this.allowEdit && this.croppedUri != null) {
                            Uri croppedUri = Uri.parse(croppedFilePath);
                            writeUncompressedImage(croppedUri, uri);
                        } else {
                            Uri imageUri = this.imageUri;
                            writeUncompressedImage(imageUri, uri);
                        }

                        this.callbackContext.success(uri.toString());
                    }
                } else {
                    Uri uri = Uri.fromFile(createCaptureFile(this.encodingType, System.currentTimeMillis() + ""));
                    bitmap = getScaledAndRotatedBitmap(sourceData, mimeType);

                    // Double-check the bitmap.
                    if (bitmap == null) {
                        LOG.d(LOG_TAG, "I either have an unreadable imageUri or null bitmap");
                        this.failPicture("Unable to create bitmap!");
                        return;
                    }

                    // Add compressed version of captured image to returned media store Uri
                    OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri);
                    CompressFormat compressFormat = getCompressFormatForEncodingType(encodingType);

                    bitmap.compress(compressFormat, this.mQuality, os);
                    os.close();

                    // Restore exif data to file
                    if (this.encodingType == JPEG) {
                        String exifPath;
                        exifPath = uri.getPath();
                        //We just finished rotating it by an arbitrary orientation, just make sure it's normal
                        if (rotate != ExifInterface.ORIENTATION_NORMAL)
                            exif.resetOrientation();
                        exif.createOutFile(exifPath);
                        exif.writeExifData();
                    }

                    // Send Uri back to JavaScript for viewing image
                    this.callbackContext.success(uri.toString());
                }
            } else {
                throw new IllegalStateException();
            }

            this.cleanup(this.imageUri, galleryUri, bitmap);
            bitmap = null;
            input.close();
        }
        catch (Exception e) {
            input.close();
            throw e;
        }
    }