3

我的目标是我的应用程序支持 30 (R)。我注意到调用这个时缺少一些应用程序可供选择:

baseActivity.startActivity(Intent(MediaStore.ACTION_IMAGE_CAPTURE))

当定位到 29 时,此代码会在拍照前显示几个可供选择的应用程序:

  • 原生相机应用
  • B612 相机应用程序

定位到30后,直接打开相机应用(没有选项可以选择)。

我查看了 android 11 的更改,但没有看到任何特别之处。我这边有什么需要改变的吗?

感谢阅读/帮助

4

2 回答 2

5

targetSdkVersion达到 30 后,将ACTION_IMAGE_CAPTURE仅显示预装的相机应用程序,而不显示用户安装的应用程序

于 2020-08-17T11:05:24.323 回答
1

我找到了解决方法;
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

于 2020-08-23T14:54:54.920 回答