我找到了解决方法;
TL;DR:自己阅读应用程序的 AndroidManifest.xml 以查找相机应用程序。
注意:这可能会导致您的应用被商店禁止。
第 1 步:使用 PackageManager,创建所有已授予相机权限的应用程序列表。
public static List<PackageInfo> getPackageInfosWithCameraPermission(Context context){
//Get a list of compatible apps
PackageManager pm = context.getPackageManager();
List<PackageInfo> installedPackages = pm.getInstalledPackages(PackageManager.GET_PERMISSIONS);
ArrayList<PackageInfo> cameraPermissionPackages = new ArrayList<PackageInfo>();
//filter out only camera apps
for (PackageInfo somePackage : installedPackages) {
//- A camera app should have the Camera permission
boolean hasCameraPermission = false;
if (somePackage.requestedPermissions == null || somePackage.requestedPermissions.length == 0) {
continue;
}
for (String requestPermission : somePackage.requestedPermissions) {
if (requestPermission.equals(Manifest.permission.CAMERA)) {
//Ask for Camera permission, now see if it's granted.
if (pm.checkPermission(Manifest.permission.CAMERA, somePackage.packageName) == PackageManager.PERMISSION_GRANTED) {
hasCameraPermission = true;
break;
}
}
}
if (hasCameraPermission) {
cameraPermissionPackages.add(somePackage);
}
}
return cameraPermissionPackages;
}
第 2 步:从 APK 文件(来自 PackageInfo)获取 AndroidManifest
public static Document readAndroidManifestFromPackageInfo(PackageInfo packageInfo) {
File publicSourceDir = new File(packageInfo.applicationInfo.publicSourceDir);
//Get AndroidManifest.xml from APK
ZipFile apkZipFile = new ZipFile(apkFile, ZipFile.OPEN_READ);
ZipEntry manifestEntry = apkZipFile.getEntry("AndroidManifest.xml");
InputStream manifestInputStream = apkZipFile.getInputStream(manifestEntry);
try {
Document doc = new CompressedXmlParser().parseDOM(manifestInputStream);
return doc;
} catch (Exception e) {
throw new IOException("Error reading AndroidManifest", e);
}
}
第 3 步:阅读 AndroidManifest 以找到具有正确 IntentFilter(s) 的活动
public static List<ComponentName> getCameraComponentNamesFromDocument(Document doc) {
@SuppressLint("InlinedApi")
String[] correctActions = {MediaStore.ACTION_IMAGE_CAPTURE, MediaStore.ACTION_IMAGE_CAPTURE_SECURE, MediaStore.ACTION_VIDEO_CAPTURE};
ArrayList<ComponentName> componentNames = new ArrayList<ComponentName>();
Element manifestElement = (Element) doc.getElementsByTagName("manifest").item(0);
String packageName = manifestElement.getAttribute("package");
Element applicationElement = (Element) manifestElement.getElementsByTagName("application").item(0);
NodeList activities = applicationElement.getElementsByTagName("activity");
for (int i = 0; i < activities.getLength(); i++) {
Element activityElement = (Element) activities.item(i);
String activityName = activityElement.getAttribute("android:name");
NodeList intentFiltersList = activityElement.getElementsByTagName("intent-filter");
for (int j = 0; j < intentFiltersList.getLength(); j++) {
Element intentFilterElement = (Element) intentFiltersList.item(j);
NodeList actionsList = intentFilterElement.getElementsByTagName("action");
for (int k = 0; k < actionsList.getLength(); k++) {
Element actionElement = (Element) actionsList.item(k);
String actionName = actionElement.getAttribute("android:name");
for (String correctAction : correctActions) {
if (actionName.equals(correctAction)) {
//this activity has an intent filter with a correct action, add this to the list.
componentNames.add(new ComponentName(packageName, activityName));
}
}
}
}
}
return componentNames;
}
第 4 步:创建所有相机应用程序的列表
List<> cameraApps = new ArrayList<>();
for (PackageInfo somePackage : cameraPermissionPackages) {
Document doc = readAndroidManifestFromPackageInfo(somePackage);
List<ComponentName> componentNames = getCameraComponentNamesFromDocument(doc);
if (componentNames.size() == 0) {
continue; //This is not a Camera app
}
cameraApps.add(cameraApp);
}
第 5 步:向用户展示相机应用程序列表。
只需创建一个对话框或其他东西。
我已经把它变成了一个图书馆:
https ://github.com/frankkienl/Camera11