我在 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();
}
}
}
如果需要,我可以提供更多代码或更多信息。