android - 在 Android 11 中,我无法使用相机意图获取结果代码 0 如何解决此错误?
问题描述
下面是我的代码
public static Uri launchPhotoChooser(final Fragment fragment) {
文件路径 = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "我的应用程序目录");
if (!path.exists()) {
path.mkdir();
}
File imageFile = null;
try {
imageFile = File.createTempFile("Your file Name", ".jpg", path);
} catch (IOException e) {
e.printStackTrace();
}
Context context = fragment.getContext();
Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileProvider", imageFile);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = fragment.getActivity().getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam)
{
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
cameraIntents.add(intent);
}
// Filesystem.
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType("image/*");
galleryIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
fragment.startActivityForResult(chooserIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION), REQUEST_CODE_PICTURE);
return uri;
}
解决方案
推荐阅读
- java - 有没有办法检测用户是否在whatsapp/messenger上成功发送/分享我的应用链接?
- java - 使上传方法返回字符串“上传的文件url”
- tensorflow - Tensorflow Serving:张量维度在服务时发生变化
- php - laravel 重定向路由多个旧的 slug 到新的
- node.js - readFileSync 不将 URL 读取为文件
- javascript - 如何使用 replace() 从字符串中删除逗号和 None
- c# - 处理长循环并防止“应用程序无响应”
- python - 获取python中某个文件夹的路径
- azure-devops - 如何自动化天蓝色板?
- haskell - 数组索引触发元素的重复评估?