Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
687 views
in Technique[技术] by (71.8m points)

camera - java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=0, data=null} to activity

    public void chooseFromCamera() {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

        startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        // Save the rider's current state
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putParcelable("file_uri", fileUri);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        // get the file url
        fileUri = savedInstanceState.getParcelable("file_uri");
    }

    public Uri getOutputMediaFileUri(int type) {
//        return Uri.fromFile(getOutputMediaFile(type));
        return FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", getOutputMediaFile(type));

    }

    private File getOutputMediaFile(int type) {

        // External sdcard location
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                IMAGE_DIRECTORY_NAME);

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
        } else {
            return null;
        }

        return mediaFile;
    }

    @Override
    public void onBackPressed() {

        if (checkEditProfileFrag() == true) {
            return;
        }

        super.onBackPressed();
    }

    public View getCurrView() {
        return generalFunc.getCurrentView(MyProfileActivity.this);
    }


    public String[] generateImageParams(String key, String content) {
        String[] tempArr = new String[2];
        tempArr[0] = key;
        tempArr[1] = content;

        return tempArr;
    }

    @SuppressLint("NewApi")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode != RESULT_CANCELED){

            if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE || requestCode == SELECT_PICTURE || requestCode == CROP_IMAGE) {
            if (resultCode == RESULT_OK) {

                if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
                    // successfully captured the image
                    // display it in image view
                    try {
                        cropImage(fileUri, fileUri);
                    } catch (Exception e) {
                        if (fileUri != null) {
//                            generalFunc.showMessage(getCurrView(), generalFunc.retrieveLangLBl("Some problem occurred.can't able to get cropped image.so we are uploading original captured image.", "LBL_CROP_ERROR_TXT"));
                            imageUpload(fileUri);
                        } else if (data != null) {
//                            generalFunc.showMessage(getCurrView(), generalFunc.retrieveLangLBl("Some problem occurred.can't able to get cropped image.so we are uploading original captured image.", "LBL_CROP_ERROR_TXT"));
                            imageUpload(data.getData());
                        } else {
                            generalFunc.showMessage(getCurrView(), generalFunc.retrieveLangLBl("", "LBL_ERROR_OCCURED"));

                        }
                        e.printStackTrace();

                    }
                }

                } else if (requestCode == SELECT_PICTURE) {

                    try {
                        Uri cropPictureUrl = Uri.fromFile(getOutputMediaFile(MEDIA_TYPE_IMAGE));
                        String realPathFromURI = new ImageFilePath().getPath(getActContext(), data.getData());
                        File file = new File(realPathFromURI == null ? getImageUrlWithAuthority(this, data.getData()) : realPathFromURI);
                        if (file.exists()) {
                            if (Build.VERSION.SDK_INT > 23) {
                                cropImage(FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", file), cropPictureUrl);

                            } else {
                                cropImage(Uri.fromFile(file), cropPictureUrl);
                            }

                        } else {
                            cropImage(data.getData(), cropPictureUrl);
                        }

                    } catch (Exception e) {
                        if (data != null) {
//                            generalFunc.showMessage(getCurrView(), generalFunc.retrieveLangLBl("Some problem occurred.can't able to get cropped image.so we are uploading original captured image.", "LBL_CROP_ERROR_TXT"));
                            imageUpload(data.getData());
                        } else {
                            generalFunc.showMessage(getCurrView(), generalFunc.retrieveLangLBl("", "LBL_ERROR_OCCURED"));

                        }

                        e.printStackTrace();
                    }
                } else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
                    CropImage.ActivityResult result = CropImage.getActivityResult(data);
                    Uri resultUri = result.getUri();
                    imageUpload(resultUri);
                } else if (requestCode == CROP_IMAGE) {
                    imageUpload(fileUri);
                }
            } else {
                if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
                    generalFunc.showMessage(getCurrView(), generalFunc.retrieveLangLBl("", "LBL_FAILED_CAPTURE_IMAGE_TXT"));
                } else {
                    generalFunc.showMessage(getCurrView(), generalFunc.retrieveLangLBl("", "LBL_ERROR_OCCURED"));
                }
            }
        } else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            Uri resultUri = result.getUri();
            imageUpload(resultUri);
        }
    }

    public static String getImageUrlWithAuthority(Context context, Uri uri) {
        InputStream is = null;
        if (uri.getAuthority() != null) {
            try {
                is = context.getContentResolver().openInputStream(uri);
                Bitmap bmp = BitmapFactory.decodeStream(is);
                return writeToTempImageAndGetPathUri(context, bmp).toString();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static Uri writeToTempImageAndGetPathUri(Context inContext, Bitmap inImage) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, Utils.TempProfileImageName, null);
        return Uri.parse(path);
    }


    private void cropImage(final Uri sourceImage, Uri destinationImage) {

        try {
            CropImage.activity(sourceImage)
                    .setGuidelines(CropImageView.Guidelines.ON)
                    .setMultiTouchEnabled(false)
                    .setDoneButtonText(generalFunc.retrieveLangLBl("Done", "LBL_DONE"))
                    .setCancelButtonText(generalFunc.retrieveLangLBl("Cancel", "LBL_CANCEL_TXT"))
                    .setAspectRatio(1024, 1024)
                    .setNoOutputImage(false)
                    .start(this);
        } catch (Exception e) {
            imageUpload(sourceImage);
        }
    }


    private void imageUpload(Uri fileUri) {
        if (SITE_TYPE == "Demo" && generalFunc.getJsonValue("vEmail", generalFunc.retrieveValue(Utils.USER_PROFILE_JSON)).equalsIgnoreCase("Driver@gmail.com")) {
            generalFunc.showGeneralMessage("", SITE_TYPE_DEMO_MSG);
            return;
        }

        if (fileUri == null) {
            generalFunc.showMessage(getCurrView(), generalFunc.retrieveLangLBl("", "LBL_ERROR_OCCURED"));
            return;
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...