default camera to take a picture in android
12,652
Solution 1
Uri imageUri;
final int TAKE_PICTURE = 115;
public void capturePhoto(View view) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photoFile = new File(Environment.getExternalStorageDirectory(), "Photo.png");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
imageUri = Uri.fromFile(photoFile);
startActivityForResult(intent, TAKE_PICTURE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImageUri = imageUri;
//Do what ever you want
}
}
}
Solution 2
The intent which is used to open the camera is
buttonCapturePhoto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_IMAGE);
}
});
The code which gives you the image after capturing is
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri uriImage;
InputStream inputStream = null;
if ( (requestCode == SELECT_IMAGE || requestCode == CAPTURE_IMAGE) && resultCode == Activity.RESULT_OK) {
uriImage = data.getData();
try {
inputStream = getContentResolver().openInputStream(uriImage);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
imageView.setImageBitmap(bitmap);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setAdjustViewBounds(true);
}
}
Comments
-
Sawan Modi over 1 year
How to use default camera to take a picture in android ?
-
Aleadam over 12 yearspossible duplicate of Android camera intent.
-
-
Dan F over 12 yearsThank you so much, I've been fumbling around for like an hour trying to get the camera object and preview method to work, when this is all I wanted from the beginning.
-
Nauman Zubair about 10 yearsHi Abhan, your code will show all 3rd party camera apps as well as default camera app. What if some one want to show only default camera without showing any chooser of 3rd party camera apps like Line Camera and Paper Camera?
-
Admin about 10 years@NaumanZubair It is hard to set. We have method named
SetClassName
but for that you would to know the exact class of the default camera app but as we all know that we have differentOEMs
and it may happen they use different packages for default app. So this is kind of trial and error scenario. -
Nauman Zubair about 10 yearsi found that solution, here it is stackoverflow.com/questions/19050810/…
-
Admin about 10 years@NaumanZubair I do not think so this is the solution. As you can say that this is a workaround to fulfill particular requirement. In any application we always need to think about the
User
. So leave up to theUser
what they want to choose. -
Nauman Zubair about 10 years@Abhan I understand your point, but in case of specific users and according to clients' restricted requirements it may helpful for other developers.