Android IMAGE_CAPTURE returns RESULT_CANCELED
Android IMAGE_CAPTURE returns RESULT_CANCELED
我正在尝试使用 MediaStore.ACTION_IMAGE_CAPTURE
意图拍摄个人资料照片。
启动意图的代码:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File myPicture = new File(OfflineApp.getAppContext().getFilesDir() + "/" + getResources().getString(R.string.contact_photos_dir), getResources().getString(R.string.my_photo_file_name));
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(myPicture));
startActivityForResult(cameraIntent, REQUEST_CODE_TAKE_PHOTO);
以及 onActivityResult 部分:
case REQUEST_CODE_TAKE_PHOTO:
if (resultCode == RESULT_OK) {
String path = new File(OfflineApp.getAppContext().getFilesDir() + "/" + getResources().getString(R.string.contact_photos_dir), getResources().getString(R.string.my_photo_file_name)).getAbsolutePath();
ContactManager.getInstance().updateMyImage(path);
}
break;
但是,结果代码总是RESULT_CANCELED
,只有我提供MediaStore.EXTRA_OUTPUT
,即使文件不存在。
好的。找到问题了。
由于 Camera Intent 正在启动相机应用程序,因此它无权访问我的应用程序文件夹。通过传递外部文件夹文件并将其复制到我的本地文件夹来修复它。
干杯。
您可以像这样让相机应用程序将图像保存在外部文件中,然后稍后使用那里的图像
private void captureImage() {
// You can pass an external file Uri to the intent to which the camera app can write
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// File file = new File(getCacheDir(), IMAGE_FILENAME); // this doesn't work as the camera app doesn't have permission to this apps cache dir
// File file = new File("/sdcard/image.jpg"); // avoid hardcoding file names
File file = new File(Environment.getExternalStorageDirectory(), IMAGE_FILENAME);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
// intent.setData(Uri.fromFile(file));
// intent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE_CAPTURE_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
if(requestCode == REQUEST_CODE_CAPTURE_IMAGE){
File file = new File(Environment.getExternalStorageDirectory(), IMAGE_FILENAME);
Uri uri = Uri.fromFile(file);
mImagePath = uri.getPath();
String extension = MimeTypeMap.getFileExtensionFromUrl(mImagePath);
if(extension != null){
mImageMIMEType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
} else {
mImageMIMEType = "image/jpg"; // fallback, also a best guess
}
// use image here
}
}
}
改为使用此文件:
File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
try {
File myPicture = File.createTempFile(String.valueOf(DateUtils.nowInMillis()), you_extension, storageDir);
} catch (Exception e) {
e.printStackTrace();
}
我正在尝试使用 MediaStore.ACTION_IMAGE_CAPTURE
意图拍摄个人资料照片。
启动意图的代码:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File myPicture = new File(OfflineApp.getAppContext().getFilesDir() + "/" + getResources().getString(R.string.contact_photos_dir), getResources().getString(R.string.my_photo_file_name));
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(myPicture));
startActivityForResult(cameraIntent, REQUEST_CODE_TAKE_PHOTO);
以及 onActivityResult 部分:
case REQUEST_CODE_TAKE_PHOTO:
if (resultCode == RESULT_OK) {
String path = new File(OfflineApp.getAppContext().getFilesDir() + "/" + getResources().getString(R.string.contact_photos_dir), getResources().getString(R.string.my_photo_file_name)).getAbsolutePath();
ContactManager.getInstance().updateMyImage(path);
}
break;
但是,结果代码总是RESULT_CANCELED
,只有我提供MediaStore.EXTRA_OUTPUT
,即使文件不存在。
好的。找到问题了。
由于 Camera Intent 正在启动相机应用程序,因此它无权访问我的应用程序文件夹。通过传递外部文件夹文件并将其复制到我的本地文件夹来修复它。
干杯。
您可以像这样让相机应用程序将图像保存在外部文件中,然后稍后使用那里的图像
private void captureImage() {
// You can pass an external file Uri to the intent to which the camera app can write
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// File file = new File(getCacheDir(), IMAGE_FILENAME); // this doesn't work as the camera app doesn't have permission to this apps cache dir
// File file = new File("/sdcard/image.jpg"); // avoid hardcoding file names
File file = new File(Environment.getExternalStorageDirectory(), IMAGE_FILENAME);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
// intent.setData(Uri.fromFile(file));
// intent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE_CAPTURE_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
if(requestCode == REQUEST_CODE_CAPTURE_IMAGE){
File file = new File(Environment.getExternalStorageDirectory(), IMAGE_FILENAME);
Uri uri = Uri.fromFile(file);
mImagePath = uri.getPath();
String extension = MimeTypeMap.getFileExtensionFromUrl(mImagePath);
if(extension != null){
mImageMIMEType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
} else {
mImageMIMEType = "image/jpg"; // fallback, also a best guess
}
// use image here
}
}
}
改为使用此文件:
File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
try {
File myPicture = File.createTempFile(String.valueOf(DateUtils.nowInMillis()), you_extension, storageDir);
} catch (Exception e) {
e.printStackTrace();
}