Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 this function but would like my app to only use the default camera.

Is there a way of doing this?

like image 830
dr_sulli Avatar asked Sep 26 '11 16:09

dr_sulli


Video Answer


2 Answers

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;
        }
    }
like image 187
PiyushMishra Avatar answered Nov 15 '22 02:11

PiyushMishra


    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.

like image 24
Nauman Zubair Avatar answered Nov 15 '22 00:11

Nauman Zubair