相机结果始终返回RESULT_CANCELED [英] Camera Result always returns RESULT_CANCELED
问题描述
我想用相机拍照.我只是暂时需要它们,所以请执行以下操作:
I want to take pictures with my camera. I just need them temporally so i do the following:
private void takePicture() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(this.getActivity().getPackageManager()) != null) {
try {
this.imageFile = File.createTempFile("CROP_SHOT", ".jpg", this.getActivity().getCacheDir());
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(this.imageFile));
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
this.startActivityForResult(intent, Globals.REQUEST_TAKE_PHOTO);
} catch (IOException e) {
e.printStackTrace();
}
}
}
很遗憾,我的onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.v("PictureEdit", "onActivityResult");
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK) {
if(requestCode == Globals.REQUEST_TAKE_PHOTO) {
Log.v("PictureEdit", "onActivityResult take photo");
this.startCropImage(Uri.fromFile(this.imageFile));
} else if (requestCode == Globals.REQUEST_PICK_PHOTO) {
Log.v("PictureEdit", "onActivityResult pick photo");
this.startCropImage(data.getData());
} else if (requestCode == Globals.REQUEST_CROP_PHOTO) {
Log.v("PictureEdit", "onActivityResult crop photo");
this.imageEncoded = data.getStringExtra(Globals.KEY_IMAGE_ENCODED);
this.imageView.setImageBitmap(Images.decodeImage(imageEncoded));
this.buttonSave.setEnabled(true);
} else {
Log.v("PictureEdit", "onActivityResult requestCode: " + requestCode + ", result: " + resultCode);
}
} else {
Log.v("PictureEdit", "onActivityResult resultCode is not ok: " + resultCode);
}
}
到目前为止,我在stackoverflow和Web上发现的每个解决方案都不能解决我的问题.我使用缓存目录创建临时文件:
Every solutions i found so far on stackoverflow and in the web did not solve my problem. I use the cache directory for creating a temp file:
File.createTempFile("CROP_SHOT", ".jpg", this.getActivity().getCacheDir());
我拥有正确的权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
从智能手机中拾取图像总是可以正常工作:
Picking images from my smartphone always works fine:
private void pickPicture() {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
this.startActivityForResult(intent, Globals.REQUEST_PICK_PHOTO);
}
那为什么相机总是返回RESULT_CANCELED
?
So why is the camera always returning RESULT_CANCELED
?
它出现在带有Android 6.0的Nexus 4和带有Android 4.2的三星Galaxy S III上.
It is appearing on Nexus 4 with Android 6.0 and on Samsung Galaxy S III with Android 4.2.
推荐答案
我使用缓存目录创建临时文件:
I use the cache directory for creating a temp file:
First, a third-party camera app has no rights to work with your app's private cache directory on internal storage.
第二,您不应该创建文件.相机应用程序应该创建文件.某些相机应用可能会感到困惑,并拒绝覆盖您的文件.
Second, you are not supposed to create the file. The camera app is supposed to create the file. Some camera apps may get confused and refuse to overwrite your file.
So, create a File
object pointing to a location on external storage (e.g., getExternalCacheDir()
), but do not create the file yet. Use that File
object as the basis for your EXTRA_OUTPUT
value.
还请注意,MediaStore.EXTRA_VIDEO_QUALITY
用于录制视频;对ACTION_IMAGE_CAPTURE
没有任何意义.
Also note that MediaStore.EXTRA_VIDEO_QUALITY
is for recording videos; it has no meaning for ACTION_IMAGE_CAPTURE
.
这篇关于相机结果始终返回RESULT_CANCELED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!