Can I force my application to launch the default Camera rather than offering the 'Complete action using' list?

前端 未结 5 615
一个人的身影
一个人的身影 2021-01-05 23:29

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 de

5条回答
  •  悲哀的现实
    2021-01-05 23:42

    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 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 list = getPackageManager().getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
        for (int n=0;n

提交回复
热议问题