我面临一个特殊问题,resolveActivity 返回 null。我知道已经有类似的问题并提供了解决方案,但我特别面临 android 14 的问题。我已在清单文件中添加了对意图的查询。但此问题仅在 Android 14 设备上持续存在。
这是我的清单文件的片段
<uses-feature android:name="android.hardware.camera" android:required= "true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
android:icon="@drawable/logo"
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
<application
android:name=".model.Multi_Dex"
android:allowBackup="true"
android:hardwareAccelerated="false"
...
这是用于启动相机意图的方法的片段。
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(Build.VERSION.SDK_INT>33){
takePictureIntent.setPackage(context.getPackageName());
}
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.ft.fifthwheel.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(takePictureIntent, REQUEST_CAMERA);
}
}
}
好的,所以我目前已经使用
queryIntentActivities
方法解决了这个问题。我已经获取了 ResolveInfo
对象的列表,为清单文件中提到的每个 Intent
创建了一个新的 intent
,并将每个意图添加到意图列表中。然后我创建了一个选择器意图来触发选择选项来选择我的照片,目前,我只允许相机选项拍照,但我可能会在清单文件中的queries
内添加更多意图。
这是示例代码
private void dispatchTakePictureIntent() {
File photoFile = null;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
ex.printStackTrace();
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.ft.fifthwheel.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
List<Intent> intentList = new ArrayList<>();
List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(takePictureIntent, 0);
for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;
Intent targetedIntent = new Intent(takePictureIntent);
targetedIntent.setPackage(packageName);
intentList.add(targetedIntent);
}
Intent chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1), "Choose an Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{}));
// Ensure that there's a camera activity to handle the intent
if (chooserIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
// Continue only if the File was successfully created
chooserIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(chooserIntent, REQUEST_CAMERA);
}
}
}
如果有更好的解决方案,我将不胜感激。