Android 在安卓11中,I';m无法使用相机意图获取结果代码0如何解决此错误?
下面是我的代码 公共静态Uri launchPhotoChooser(最终片段) { 文件路径= 新文件(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),“我的应用程序目录”)Android 在安卓11中,I';m无法使用相机意图获取结果代码0如何解决此错误?,android,android-intent,android-camera,android-11,scoped-storage,Android,Android Intent,Android Camera,Android 11,Scoped Storage,下面是我的代码 公共静态Uri launchPhotoChooser(最终片段) { 文件路径= 新文件(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),“我的应用程序目录”) 如果(!path.exists()){ path.mkdir(); } File imageFile=null; 试一试{ imageFile=File.createTempFile(“您的文件名”、“.jpg”、
如果(!path.exists()){
path.mkdir();
}
File imageFile=null;
试一试{
imageFile=File.createTempFile(“您的文件名”、“.jpg”、路径);
}捕获(IOE异常){
e、 printStackTrace();
}
Context=fragment.getContext();
Uri Uri=FileProvider.getUriForFile(context,context.getApplicationContext().getPackageName()+“.FileProvider”,imageFile);
//照相机。
最终列表cameraIntents=newarraylist();
最终意图捕获意图=新意图(android.provider.MediaStore.ACTION\u IMAGE\u CAPTURE);
最终PackageManager PackageManager=fragment.getActivity().getPackageManager();
最终列表listCam=packageManager.QueryInputActivities(captureIntent,0);
用于(ResolveInfo res:listCam)
{
最后一个字符串packageName=res.activityInfo.packageName;
最终意图=新意图(captureIntent);
setComponent(新组件名(res.activityInfo.packageName,res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_输出,uri);
添加(意图);
}
//文件系统。
Intent gallerycontent=新意图(Intent.ACTION\u PICK,
android.provider.MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI);
GalleryContent.setType(“image/*”);
gallerycontent.putExtra(“outputFormat”,Bitmap.CompressFormat.JPEG.toString());
//选择文件系统选项。
最终意图选择器content=Intent.createChooser(GalleryContent,“选择源”);
//添加相机选项。
选择content.putExtra(Intent.EXTRA\u INITIAL\u INTENTS,cameraIntents.toArray(新包裹[cameraIntents.size()]);
fragment.startActivityForResult(选择content.addFlags(Intent.FLAG\u GRANT\u READ\u URI\u PERMISSION)、REQUEST\u CODE\u PICTURE);
返回uri;
}
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;
}