android - 为什么android Intent ACTION_IMAGE_CAPTURE 结果代码总是0(CANCELLED)
问题描述
我有一个应用程序,用户可以在其中拥有个人资料图片,他们还可以提交照片以贡献某些项目信息。但我不明白为什么我的相机意图总是返回一个“取消”的结果代码。
我通过选择器意图创建意图,他们可以在其中选择他们的照片应用程序,如果他们从画廊或其他任何地方选择图像,它就可以正常工作。我有一个具有此功能的类 PhotoTaker,该功能在照片工厂按钮单击时调用:
public void startTakingSinglePhoto(View view) {
if (!EasyPermissions.hasPermissions(getApplicationContext(), PERMS)) {
isAskPermissionOnTakingSinglePhoto = true;
EasyPermissions.requestPermissions(this, getResources().getString(R.string.popup_title_permission_files_access), RC_TAKE_PHOTO, PERMS);
return;
}
Intent i = getCameraIntent();
startActivityForResult(i, RC_TAKE_PHOTO);
}
这里是getCameraIntent
:
private Intent getCameraIntent() {
Intent cameraIntent= new Intent(ACTION_IMAGE_CAPTURE);
//Create the local file where will be store camera picture
File photoFile;
photoFile = createImageFile();
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this, AUTHORITIES, photoFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
}else{
Log.e("=>", "photo file is null");
}
return cameraIntent;
}
这是createImageFile
:
private File createImageFile() {
// Create an image file name
String timeStamp = new SimpleDateFormat("dd/MM/yyyy hh:mm").format(new Date());
String imageFileName = "RideMonPark_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = new File(storageDir, imageFileName + ".jpg");
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
然后我有一个类GalleryActivity
,用于创建一个包含用户提交的所有图像的画廊。它扩展PhotoTaker
了上面所有的代码。
onActivityResult
在这里GalleryActivity
:
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.w("=>", "gallery activity result : " + requestCode + " : " + resultCode);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case RC_TAKE_PHOTO:
Toast.makeText(this, getResources().getString(R.string.sending_picture), Toast.LENGTH_LONG).show();
Log.w("=>", "intent : " + data);
if(data == null || data.getData() == null){
//FROM CAMERA add current path file uri
Log.w("=>", "from camera : " + mCurrentPhotoPath);
addPhotoInGallery(mCurrentPhotoPath);
}else{
//From gallery, add File Uri
Log.w("=>", "from gallery : " + data.getData().toString());
addPhotoInGallery(data.getData().toString());
}
break;
}
}
}
我从来没有机会进入onActivityResult
,因为第一个日志显示带有相机应用程序的结果代码为 0。
我没有足够的带宽下载另一个 AVD,所以我只在我的 Honor8 和 Virtual Google Pixel 2 上进行了测试。
我读过很多书,因为几乎每部手机都有自己不同的相机应用程序,所以依赖用户相机应用程序并不是一个好主意。我应该自己做吗?我是这个应用程序的唯一开发者,而且我的时间很短。我认为为应用程序制作相机不值得,但如果这是让它在所有设备上工作的唯一解决方案,我会这样做......
编辑:这是清单中的我的提供者标签:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.name.example"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
还有我的 file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="my_images"
path="Android/data/com.name.example/files/Pictures"/>
</paths>
解决方案
推荐阅读
- c# - 使用 Visual Studio 2015 构建将类库 app.config 重命名为
.config - react-native - 如何在 React Native 组件之外获取样式的智能感知
- python - 是否可以时间同步 ROS 中相同消息类型的两个主题?
- php - Api-Platform 在 api_platform.yaml 中缺少“服务器”配置键
- sqlite - Sqlite外键一字段两表
- swift - 如何修复 loadPersistentStores 在加载时崩溃
- kubernetes - 通过 helm 部署 Kubernetes 作业
- node.js - 环境变量文件的命名约定?
- c++ - 在 Android 6.0 上加载 protobuf-lite.so 时,如何使用 UnsatisfiedLinkError 修复 Android NDK 应用程序崩溃?
- animation - Maxscript:如何更改键的值?