1

我在 Android 中遇到了一些奇怪的行为。我正在尝试捕获图像,它适用于 Android 5.0 及更高版本,包括 Android 7.0。这是我关注的链接 - Link1 Link2

奇怪的行为发生在 Android 4.4.2 (kitkat) 上。它允许我单击图片,然后单击“确定”,onActivityResult 中的 resultCode 始终为 0 (RESULT_CANCELED)。但它在棒棒糖及更高版本上正确返回 -1 (RESULT_OK)。我的意思是我点击了确定,为什么它返回 RESULT_CANCELED。

这里有什么问题?

这是一些代码:

private String mCurrentPhotoPath;

private void dispatchTakePictureIntent() throws IOException {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            return;
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(ImageCropActivity.this,
                    "myFileProviderName",
                    photoFile);

            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQ_CODE_PERMISSIONS_CAPTURE);
        }
    } else {
        Log.e(TAG, "Could not start camera. Intent(MediaStore.ACTION_IMAGE_CAPTURE) could not be resolved");
        //handle failure here
    }
}



private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DCIM), "Camera");
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

//previously - mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    mCurrentPhotoPath = image.getAbsolutePath();

    return image;
}




@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQ_CODE_PERMISSIONS_CAPTURE) {
        if(resultCode == RESULT_OK) {
            Log.i(TAG, "Picture taken");
            // Show the thumbnail on ImageView
            Uri imageUri = Uri.parse(mCurrentPhotoPath);
            mImageUri = imageUri;
            initImage();

            // ScanFile so it will be appeared on Gallery
            MediaScannerConnection.scanFile(ImageCropActivity.this,
                    new String[]{imageUri.getPath()}, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        public void onScanCompleted(String path, Uri uri) {
                        }
                    });
        }
        else if(resultCode == RESULT_CANCELED){
            Log.i(TAG, "Picture not taken");
           finish();
        }
        else {
            errored();
        }
    }
}

如果需要,我可以提供更多代码或更多信息。

4

1 回答 1

2

我通过进行以下更改解决了我的问题...

private void takePictureFromCamera() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        photoFile = createImageFile();
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(activity,
                    "com.example.provider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);

            //COMPATIBILITY
            if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.LOLLIPOP) {
                takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            } else {
                List<ResolveInfo> resInfoList = activity.getPackageManager().queryIntentActivities(takePictureIntent, PackageManager.MATCH_DEFAULT_ONLY);
                for (ResolveInfo resolveInfo : resInfoList) {
                    String packageName = resolveInfo.activityInfo.packageName;
                    activity.grantUriPermission(packageName, photoURI, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                }
            }
            //COMPATIBILITY
            startActivityForResult(takePictureIntent, CAMERA);
        }


    }
}

我在以下位置找到了解决方案:

关联

于 2018-03-12T14:29:58.237 回答