摄像机意图resultCode 0在4.4.2中但-1在5.0及以上

时间:2017-03-16 15:05:57

标签: android android-camera android-camera-intent android-image-capture

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

奇怪的行为发生在Android 4.4.2(kitkat)上。它允许我点击图片然后点击"好的"和onActivityResult中的resultCode始终为0(RESULT_CANCELED)。但它正确地在棒棒糖及以上返回-1(RESULT_OK)。我的意思是我点击了Okay,为什么它会返回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();
        }
    }
}

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

1 个答案:

答案 0 :(得分:1)

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

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);
        }


    }
}

我找到了解决方案:

link