2

我希望用户能够在我的应用中快速打开默认相机应用(或他们设置为默认的应用)。但是,我不想使用 android.media.action.IMAGE_CAPTURE,因为这只会显示应用程序的拍照部分。我只想简单地打开相机应用程序而不使用它。我确实知道这是可能的,因为我使用过的几个图库应用程序(最重要的是:Focus)能够简单地打开相机应用程序而没有问题,并且没有使用 IMAGE_CAPTURE。

4

3 回答 3

4

这可以通过使用来实现PackageManager#resolveActivity(Intent)

在科特林:

val info: ResolveInfo? = packageManager
        .resolveActivity(cameraIntent);
if (info == null) {
    // No camera app installed.
    return
}

// Documentation says at least one of the three infos is not-null:
val app: ApplicationInfo = info.activityInfo?.applicationInfo 
        ?: info.serviceInfo?.applicationInfo 
        ?: info.providerInfo!!.applicationInfo 
val launch: Intent? = packageManager
        .getLaunchIntentForPackage(app.packageName)
if (launch == null) {
    // Camera app has no default intent.
    return
}
// Launch the camera intent's 
// resolved app's default activity.
context.startActivity(launch)

(使用过滤器cameraIntent创建的 Intent 在哪里,是当前应用程序上下文并且是上下文的实例。)android.media.action.IMAGE_CAPTUREcontextpackageManagerPackageManager

于 2017-06-01T02:54:56.530 回答
2

如果您只想打开默认相机应用程序,请使用下面的代码

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        startActivity(
                getPackageManager().getLaunchIntentForPackage(

intent.resolveActivity(getPackageManager()).getPackageName()));
于 2017-10-07T09:14:29.687 回答
0
       String intentpackage;
        List<ApplicationInfo> list = packageManager.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
            for (int a=0;a<list.size();a++) {
                if((list.get(n).flags & ApplicationInfo.FLAG_SYSTEM)==1)
                {
                    Log.d("TAG", "Installed Applications  : " + list.get(a).loadLabel(packageManager).toString());
                    Log.d("TAG", "package name  : " + list.get(a).packageName);
                    if(list.get(a).loadLabel(packageManager).toString().equalsIgnoreCase("Camera")) {
                        intentpackage = list.get(a).packageName;
                        break;
                    }
                }
            }

 Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if(defaultCameraPackage!=null){
                cameraIntent.setPackage(intentpackage);
            }

            startActivityForResult(cameraIntent, 1);
于 2018-09-10T07:15:24.240 回答