我可以强制我的应用程序启动默认相机而不是提供“使用完成操作”吗?列表?

发布于 2024-12-06 20:35:58 字数 154 浏览 1 评论 0原文

我正在使用意图 new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 来捕获图像以在我的应用程序中使用。我的设备上安装了两个可以执行此功能的应用程序,但希望我的应用程序仅使用默认摄像头。

有办法做到这一点吗?

I'm using the intent new Intent(MediaStore.ACTION_IMAGE_CAPTURE); to capture an image for use in my application. I have two applications installed on my device that can perform this function but would like my app to only use the default camera.

Is there a way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

染火枫林 2024-12-13 20:35:58

正如 Dr_sulli 所建议的,我只是将其转换为代码,它对我来说效果很好,如果需要访问直接相机应用程序,其他部分是允许用户选择其他相机应用程序以及系统相机。

protected static final int CAMERA_ACTIVITY = 100;

Intent mIntent = null;
        if(isPackageExists("com.google.android.camera")){
            mIntent= new Intent();
            mIntent.setPackage("com.google.android.camera");
            mIntent.setAction(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            mIntent.putExtra("output", Uri.fromFile(new File(Environment
                    .getExternalStorageDirectory(), "/myImage" + ".jpg")));
        }else{
        mIntent = new Intent(
                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        mIntent.putExtra("output", Uri.fromFile(new File(Environment
                .getExternalStorageDirectory(), "/myImage" + ".jpg")));

        Log.i("in onMenuItemSelected",
                "Result code = "
                        + Environment.getExternalStorageDirectory());
        }
        startActivityForResult(mIntent, CAMERA_ACTIVITY);

在 onActivityResult 中做你的事情

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    Log.i("in onActivityResult", "Result code = " + resultCode);
    if (resultCode == -1) {
        switch (requestCode) {
        case CAMERA_ACTIVITY:
            //do your stuff here, i am just calling the path of stored image
            String filePath = Environment.getExternalStorageDirectory()
                    + "/myImage" + ".jpg";
                    }
                     }
                 }

isPackageExists 将确认包是否存在。

public boolean isPackageExists(String targetPackage){
    List<ApplicationInfo> packages;
    PackageManager pm;
        pm = getPackageManager();        
        packages = pm.getInstalledApplications(0);
        for (ApplicationInfo packageInfo : packages) {
    if(packageInfo.packageName.equals(targetPackage)) return true;
    }        
    return false;
}

或者您可以按照我的方式进行操作,这要容易得多,这将过滤所有系统应用程序,然后您比较名称,因此它适用于所有手机,但由于硬编码,上述技术并不适用于每部手机。稍后您可以使用此包名称来启动相机活动,如上所述。

PackageManager pm = this.getPackageManager();

    List<ApplicationInfo> list = getPackageManager().getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
    for (int n=0;n<list.size();n++) {
        if((list.get(n).flags & ApplicationInfo.FLAG_SYSTEM)==1)
        {
            Log.d("Installed Applications", list.get(n).loadLabel(pm).toString());
            Log.d("package name", list.get(n).packageName);
            if(list.get(n).loadLabel(pm).toString().equalsIgnoreCase("Camera"))
                break;
        }
    }

As Dr_sulli suggested, i am just converting it into a code and it works for me well, If case to access direct camera application and else part is allow the user to choose other camera applications along with system camera.

protected static final int CAMERA_ACTIVITY = 100;

Intent mIntent = null;
        if(isPackageExists("com.google.android.camera")){
            mIntent= new Intent();
            mIntent.setPackage("com.google.android.camera");
            mIntent.setAction(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            mIntent.putExtra("output", Uri.fromFile(new File(Environment
                    .getExternalStorageDirectory(), "/myImage" + ".jpg")));
        }else{
        mIntent = new Intent(
                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        mIntent.putExtra("output", Uri.fromFile(new File(Environment
                .getExternalStorageDirectory(), "/myImage" + ".jpg")));

        Log.i("in onMenuItemSelected",
                "Result code = "
                        + Environment.getExternalStorageDirectory());
        }
        startActivityForResult(mIntent, CAMERA_ACTIVITY);

inside onActivityResult do your stuff

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    Log.i("in onActivityResult", "Result code = " + resultCode);
    if (resultCode == -1) {
        switch (requestCode) {
        case CAMERA_ACTIVITY:
            //do your stuff here, i am just calling the path of stored image
            String filePath = Environment.getExternalStorageDirectory()
                    + "/myImage" + ".jpg";
                    }
                     }
                 }

isPackageExists will confirm the package exist or not.

public boolean isPackageExists(String targetPackage){
    List<ApplicationInfo> packages;
    PackageManager pm;
        pm = getPackageManager();        
        packages = pm.getInstalledApplications(0);
        for (ApplicationInfo packageInfo : packages) {
    if(packageInfo.packageName.equals(targetPackage)) return true;
    }        
    return false;
}

OR you can do it in my way its much easier, this will filter the all system application and then later you compare the name hence it work on all phone but the above technique due to hard coding will not work on every phone. Later you can use this package name to start the camera activity as i described above.

PackageManager pm = this.getPackageManager();

    List<ApplicationInfo> list = getPackageManager().getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
    for (int n=0;n<list.size();n++) {
        if((list.get(n).flags & ApplicationInfo.FLAG_SYSTEM)==1)
        {
            Log.d("Installed Applications", list.get(n).loadLabel(pm).toString());
            Log.d("package name", list.get(n).packageName);
            if(list.get(n).loadLabel(pm).toString().equalsIgnoreCase("Camera"))
                break;
        }
    }
虫児飞 2024-12-13 20:35:58
    List<ApplicationInfo> list = packageManager.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
    for (int n=0;n<list.size();n++) {
        if((list.get(n).flags & ApplicationInfo.FLAG_SYSTEM)==1)
        {
            Log.d("TAG", "Installed Applications  : " + list.get(n).loadLabel(packageManager).toString());
            Log.d("TAG", "package name  : " + list.get(n).packageName);
            if(list.get(n).loadLabel(packageManager).toString().equalsIgnoreCase("Camera")) {
                defaultCameraPackage = list.get(n).packageName;
                break;
            }
        }
    }

我发现以下解决方案及其工作完美。

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.setPackage(defaultCameraPackage); 
startActivityForResult(takePictureIntent, actionCode);

您可以通过在上面的意图中设置包来过滤默认相机。我通过安装两个应用程序线相机纸相机进行了测试,这两个应用程序都显示选择器,但按上面的代码包仅打开默认相机。

    List<ApplicationInfo> list = packageManager.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
    for (int n=0;n<list.size();n++) {
        if((list.get(n).flags & ApplicationInfo.FLAG_SYSTEM)==1)
        {
            Log.d("TAG", "Installed Applications  : " + list.get(n).loadLabel(packageManager).toString());
            Log.d("TAG", "package name  : " + list.get(n).packageName);
            if(list.get(n).loadLabel(packageManager).toString().equalsIgnoreCase("Camera")) {
                defaultCameraPackage = list.get(n).packageName;
                break;
            }
        }
    }

I find following solution and its working perfectly.

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.setPackage(defaultCameraPackage); 
startActivityForResult(takePictureIntent, actionCode);

you can filter default camera by setting package in above intent. I've tested it by installing two application Line Camera and Paper Camera both apps were showing chooser but filtering by package above code open only default camera.

梦幻的味道 2024-12-13 20:35:58

我准备了一个适用于几乎所有解决方案的解决方案:

  private void openCamera() {
        String systemCamera;
        if (isPackageExists("com.google.android.camera")) {
            systemCamera = "com.google.android.camera";
        } else {
            systemCamera = findSystemCamera();
        }

        if (systemCamera != null) {
            Log.d(TAG, "System Camera: " + systemCamera);
            Intent mIntent = new Intent();
            mIntent.setPackage(systemCamera);
            File f = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), ("IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".jpg"));
            mIntent.setAction(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            mIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
            startActivityForResult(mIntent, IMAGE_CODE_PICKER);
        } else {
            //Unable to find default camera app 
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IMAGE_CODE_PICKER && resultCode == RESULT_OK) {

        }
    }

    private boolean isPackageExists(String targetPackage) {
        List<ApplicationInfo> packages;
        PackageManager pm;
        pm = getPackageManager();
        packages = pm.getInstalledApplications(0);
        for (ApplicationInfo packageInfo : packages) {
            if (packageInfo.packageName.equals(targetPackage)) {
                return true;
            }
        }
        return false;
    }

    private String findSystemCamera() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        PackageManager packageManager = this.getPackageManager();
        List<ResolveInfo> listCam = packageManager.queryIntentActivities(intent, 0);
        for (ResolveInfo info :
                listCam) {
            if (isSystemApp(info.activityInfo.packageName)) {
                return info.activityInfo.packageName;
            }
        }
        return null;
    }


    boolean isSystemApp(String packageName) {
        ApplicationInfo ai;
        try {
            ai = this.getPackageManager().getApplicationInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return false;
        }
        int mask = ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
        return (ai.flags & mask) != 0;
    }

您可能需要在 onCreate 方法中添加以下代码:

StrictMode.VmPolicy.Builder newbuilder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(newbuilder.build());

I have prepared a solution which works in almost all solution:

  private void openCamera() {
        String systemCamera;
        if (isPackageExists("com.google.android.camera")) {
            systemCamera = "com.google.android.camera";
        } else {
            systemCamera = findSystemCamera();
        }

        if (systemCamera != null) {
            Log.d(TAG, "System Camera: " + systemCamera);
            Intent mIntent = new Intent();
            mIntent.setPackage(systemCamera);
            File f = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), ("IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".jpg"));
            mIntent.setAction(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            mIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
            startActivityForResult(mIntent, IMAGE_CODE_PICKER);
        } else {
            //Unable to find default camera app 
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IMAGE_CODE_PICKER && resultCode == RESULT_OK) {

        }
    }

    private boolean isPackageExists(String targetPackage) {
        List<ApplicationInfo> packages;
        PackageManager pm;
        pm = getPackageManager();
        packages = pm.getInstalledApplications(0);
        for (ApplicationInfo packageInfo : packages) {
            if (packageInfo.packageName.equals(targetPackage)) {
                return true;
            }
        }
        return false;
    }

    private String findSystemCamera() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        PackageManager packageManager = this.getPackageManager();
        List<ResolveInfo> listCam = packageManager.queryIntentActivities(intent, 0);
        for (ResolveInfo info :
                listCam) {
            if (isSystemApp(info.activityInfo.packageName)) {
                return info.activityInfo.packageName;
            }
        }
        return null;
    }


    boolean isSystemApp(String packageName) {
        ApplicationInfo ai;
        try {
            ai = this.getPackageManager().getApplicationInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return false;
        }
        int mask = ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
        return (ai.flags & mask) != 0;
    }

You may need to put following code in onCreate method:

StrictMode.VmPolicy.Builder newbuilder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(newbuilder.build());
萌化 2024-12-13 20:35:58

我不确定您想要完成的任务是否可以跨所有设备移植。 Android 的目标之一是允许轻松替换服务请求(例如图像捕获)的活动。话虽这么说,如果您只想为您的设备执行此操作,您可以简单地将意图中的组件名称设置为媒体捕获活动的名称。

I'm not sure what you are trying to accomplish is portable across all devices. One goal of Android is to allow the easy replacement of activities that service requests such as image capture. That being said, if you only wish to do this for your device, you could simply set the component name in the intent to the media capture activity's name.

最初的梦 2024-12-13 20:35:58

默认相机应用程序是安装的第一个应用程序。因此,camlist 中的第一个元素是默认应用程序

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    PackageManager packageManager = MapsActivity.this.getPackageManager();
    List<ResolveInfo> listCam = packageManager.queryIntentActivities(intent, 0);
    intent.setPackage(listCam.get(0).activityInfo.packageName);
    startActivityForResult(intent,INTENT_REQUESTCODE1);

the default camera app is the first app was installed. So, the first element in camlist is the default app

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    PackageManager packageManager = MapsActivity.this.getPackageManager();
    List<ResolveInfo> listCam = packageManager.queryIntentActivities(intent, 0);
    intent.setPackage(listCam.get(0).activityInfo.packageName);
    startActivityForResult(intent,INTENT_REQUESTCODE1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文