Previously in Android when you want to capture an Image, you have to use SurfaceView and SurfaceHolder to preview the camera. MediaStore.ACTION_IMAGE_CAPTURE was introduced in Cupcake, Android 1.5, and has been the default way to capture images captured in Android.
What Do I Need
In Manafiest.xml
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
In your activity
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile(this) );
startActivityForResult(intent, TAKE_PHOTO_CODE);
This is the core of the code, here we call an intent from the MediaStore which would open up the camera app, then we pass the output path of the captured image to a temporary location (Always use a safe location to store the image). Here we use the Environment.getExternalStorageDirectory() which is our SDCard, again for safety reason check if the SDCard is present or not. Then we start the activity, expecting a result with code TAKE_PHOTO_CODE
final File file = getTempFile(this);
try {
Bitmap captureBmp = Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
}...{}
Knowing where the output of the file will be, we would open that file and place it on a bitmap where we would do our magic
Conclusion
What Do I Need
In Manafiest.xml
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
In your activity
private static final int TAKE_PHOTO_CODE = 1;
private void takePhoto(){
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(this)) );
startActivityForResult(intent, TAKE_PHOTO_CODE);
}
private File getTempFile(Context context){
//it will return /sdcard/image.tmp
final File path = new File( Environment.getExternalStorageDirectory(), context.getPackageName() );
if(!path.exists()){
path.mkdir();
}
return new File(path, "image.tmp");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch(requestCode){
case TAKE_PHOTO_CODE:
final File file = getTempFile(this);
try {
Bitmap captureBmp = Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
// do whatever you want with the bitmap (Resize, Rename, Add To Gallery, etc)
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
Ads from Amazon:
Explanation
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile(this) );
startActivityForResult(intent, TAKE_PHOTO_CODE);
This is the core of the code, here we call an intent from the MediaStore which would open up the camera app, then we pass the output path of the captured image to a temporary location (Always use a safe location to store the image). Here we use the Environment.getExternalStorageDirectory() which is our SDCard, again for safety reason check if the SDCard is present or not. Then we start the activity, expecting a result with code TAKE_PHOTO_CODE
final File file = getTempFile(this);
try {
Bitmap captureBmp = Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
}...{}
Knowing where the output of the file will be, we would open that file and place it on a bitmap where we would do our magic
Conclusion
ACTION_IMAGE_CAPTURE has done a great job so that we wont implement our own implementation of the camera app, so use it.
原始链接:http://www.tutorialforandroid.com/2010/10/take-picture-in-android-with.html