In Android, there are two ways to take photos. One is to call the camera that comes with the system and then use the photo data it returns. Another method is to use the Camera class and other related classes to implement Camera functions. This method has a relatively high system and is complicated for dyeing. Generally, you only need to use the first method for common applications.
Code for starting the camera with Intent:
Intent intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE );
StartActivityForResult (intent, 1); after taking a picture, you can get the Bitmap object in onActivityResult (int requestCode, int resultCode, Intent data. Bitmap bitmap = (Bitmap) data. getExtras (). get ("data ");
Before storing images to the SD card, check whether the SD card is available.
String sdStatus = Environment. getExternalStorageState ();
If (! SdStatus. equals (Environment. MEDIA_MOUNTED) {// check whether sd is available
Log. v ("TestFile ",
"SD card is not avaiable/writeable right now .");
Return;
}
Run the following code to save the image file to the "sdcard/myImage/images" folder named "2017111.jpg"
File file = new File ("/sdcard/myImage /");
File. mkdirs (); // create a folder
String fileName = "/sdcard/myImage/111.jpg ";
Try {
B = new FileOutputStream (fileName );
Bitmap. compress (Bitmap. CompressFormat. JPEG, 100, B); // write data to a file
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Finally {
Try {
B. flush ();
B. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
In addition, you must first configure the permission in the Mainifest. xml file to read and write the SD card file:
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
<Uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/>
A demo that calls the System camera to take a photo, display it on the screen, and save it to the SD card.
The complete code is as follows:
MyCaremaActivity. java
Package barry. android. c;
Import java. io. File;
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import android. app. Activity;
Import android. content. Intent;
Import android. graphics. Bitmap;
Import android. OS. Bundle;
Import android. OS. Environment;
Import android. provider. MediaStore;
Import android. util. Log;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. ImageView;
Public class MyCaremaActivity extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button button = (Button) findViewById (R. id. button );
Button. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Intent intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE );
StartActivityForResult (intent, 1 );
}
});
}
@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );
If (resultCode = Activity. RESULT_ OK ){
String sdStatus = Environment. getExternalStorageState ();
If (! SdStatus. equals (Environment. MEDIA_MOUNTED) {// check whether sd is available
Log. v ("TestFile ",
"SD card is not avaiable/writeable right now .");
Return;
}
Bundle bundle = data. getExtras ();
Bitmap bitmap = (Bitmap) bundle. get ("data"); // obtain the data returned by the camera and convert it to the Bitmap image format.
FileOutputStream B = null;
File file = new File ("/sdcard/myImage /");
File. mkdirs (); // create a folder
String fileName = "/sdcard/myImage/111.jpg ";
Try {
B = new FileOutputStream (fileName );
Bitmap. compress (Bitmap. CompressFormat. JPEG, 100, B); // write data to a file
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Finally {
Try {
B. flush ();
B. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
(ImageView) findViewById (R. id. imageView). setImageBitmap (bitmap); // display the image in ImageView
}
}
}
Main. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<Button
Android: id = "@ + id/button"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "click to start the camera"/>
<ImageView
Android: id = "@ + id/imageView"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "#999999"/>
</LinearLayout>
AndroidMainifest. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "barry. android. c"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Uses-sdk android: minSdkVersion = "7"/>
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
<Uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/>
<Application
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name">
<Activity
Android: label = "@ string/app_name"
Android: name = ". MyCaremaActivity">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>
</Manifest>