Skip to content Skip to sidebar Skip to footer

Can I Force My Application To Launch The Default Camera Rather Than Offering The 'complete Action Using' List?

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 thi

Solution 1:

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.

protectedstaticfinalintCAMERA_ACTIVITY=100;

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

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

inside onActivityResult do your stuff

protectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stubsuper.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 imageStringfilePath= Environment.getExternalStorageDirectory()
                    + "/myImage" + ".jpg";
                    }
                     }
                 }

isPackageExists will confirm the package exist or not.

publicbooleanisPackageExists(String targetPackage){
    List<ApplicationInfo> packages;
    PackageManager pm;
        pm = getPackageManager();        
        packages = pm.getInstalledApplications(0);
        for (ApplicationInfo packageInfo : packages) {
    if(packageInfo.packageName.equals(targetPackage)) returntrue;
    }        
    returnfalse;
}

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;
        }
    }

Solution 2:

    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.

IntenttakePictureIntent=newIntent(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.

Solution 3:

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

privatevoidopenCamera() {
        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 = newIntent();
            mIntent.setPackage(systemCamera);
            File f = newFile(getExternalFilesDir(Environment.DIRECTORY_PICTURES), ("IMG_" + newSimpleDateFormat("yyyyMMdd_HHmmss").format(newDate()) + ".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 
        }
    }

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

        }
    }

    privatebooleanisPackageExists(String targetPackage) {
        List<ApplicationInfo> packages;
        PackageManager pm;
        pm = getPackageManager();
        packages = pm.getInstalledApplications(0);
        for (ApplicationInfo packageInfo : packages) {
            if (packageInfo.packageName.equals(targetPackage)) {
                returntrue;
            }
        }
        returnfalse;
    }

    privateStringfindSystemCamera() {
        Intent intent = newIntent(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;
            }
        }
        returnnull;
    }


    booleanisSystemApp(String packageName) {
        ApplicationInfo ai;
        try {
            ai = this.getPackageManager().getApplicationInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            returnfalse;
        }
        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.Buildernewbuilder=newStrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(newbuilder.build());

Solution 4:

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.

Solution 5:

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

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

Post a Comment for "Can I Force My Application To Launch The Default Camera Rather Than Offering The 'complete Action Using' List?"