Android - OnActivityResult()resultCode为0且Intent在Camera Intent

时间:2018-01-16 12:47:56

标签: android android-intent onactivityresult

我正在开发一款Android应用程序,该应用程序使用Intent打开内置摄像头,并拍摄一张照片,然后将照片保存到存储中并发送到另一个用于裁剪它的目的。

我已经运行了几次应用,但无法在存储中的任何位置看到任何照片被保存。我开始调试代码,发现它正在跳过OnActivityResult方法中的命令,因为resultCode参数总是返回0。 出于好奇,我手动将其更改为所需的值-1,然后代码跳过下一个if语句,因为data.getExtras()data是Intent参数)返回null,即使我在触发Camera Activity的代码中添加了额外的内容。

我无法弄清楚这里发生了什么。我假设图像以某种方式保存,因为没有抛出任何异常,但是没有看到可能导致resultCodedata空值的原因。

我的代码如下所示。如果有人能给我一个正确的方向,那将是一个巨大的帮助!

谢谢, 标记

        public void onClick(DialogInterface dialogInterface, int i) {
            if (optionItems[i].equals(OPTION_CAMERA)) {
                //intent to open device camera
                Intent cameraIntent =
                        new Intent(
                                MediaStore.ACTION_IMAGE_CAPTURE
                        );

                //create 'Spond' folder inside photo directory
                //target folder for image to be saved
                File imagesFolder =
                        new File(
                                Environment.getExternalStoragePublicDirectory(
                                        Environment.DIRECTORY_PICTURES),
                                getString(R.string.app_name)
                        );
                //create directory if it doesn't already exist
                imagesFolder.mkdirs(); //bool return value not needed

                //create file with unique id
                File image = createFileAtUniquePath(imagesFolder);
                //use authority for AndroidManifest to create permission
                //to get uri from temporary file in app
                String fileProviderAuthority = getApplicationContext().getPackageName() +
                        getString(R.string.authorities_fileprovider);
                Uri uriSavedImage = FileProvider.getUriForFile(
                        getApplicationContext(),
                        fileProviderAuthority,
                        image
                );

                //pass URI to intent so it will be available in activity result
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
                //grant read/write permissions with file
                cameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                cameraIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                startActivityForResult(cameraIntent, REQUEST_CAMERA);

OnActivityResult代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {
            if (data.getExtras() != null) {
                newAccountPhotoUri = (Uri) data.getExtras().get(MediaStore.EXTRA_OUTPUT);
                newAccountPhotoFileName = getFileNameFromURI(newAccountPhotoUri);
                if (!newAccountPhotoFileName.equals("")) {
                    appAccountPhotoChanged = true;
                    //send image to be cropped
                    cropImage(newAccountPhotoUri);
                }
            }
        }
        //...
    }

0 个答案:

没有答案