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?
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.
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;
}
}
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.
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);
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());