In Android, photographic systems are available and can be used directly in apps. When a phone downloads a camera app from Android play, it determines whether the phone supports it. Not supported, do not give download.
There are several steps to taking photos:
1. Declaring permissions
2. Use camera Photography
3. Display Picture
1. Declaring permissions
Declare the use of camera in manifest:
Copy Code code as follows:
<uses-feature android:name= "Android.hardware.camera"/>
2. Use camera Photography
In the activity, invoke the camera application
Copy Code code as follows:
private void dispatchtakepictureintent (int actioncode) {
Intent takepictureintent = new Intent (mediastore.action_image_capture);
Startactivityforresult (Takepictureintent, Actioncode);
}
3. Display Picture
After using the camera, you will return to the picture, you must first get the picture, and then show it.
Obtained in the Onactivityresult method
Copy Code code as follows:
<pre Class=java name= "code" > @Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
Switch (requestcode) {
Bundle extras = Intent.getextras ();
Bitmap Mimagebitmap = (Bitmap) extras.get ("Data");
Mimageview.setimagebitmap (MIMAGEBITMAP);
}</pre>
<PRE></PRE>
<PRE></PRE>
To save a picture to create a directory, to start the camera application, you need to specify the file
Copy code code as follows:
Intent takepictureintent = new Intent (mediastore.action_image_capture);
File f = null;
try {&nbs P
f = setupphotofile ();
Takepictureintent.putextra (Mediastore.extra_output, Uri.fromfile (f));
} catch (IOException e) {
& nbsp; e.printstacktrace ();
f = null;
}
Copy Code code as follows:
Private File Createimagefile () throws IOException {
Create an image file name
String TimeStamp = new SimpleDateFormat ("Yyyymmdd_hhmmss"). Format (new Date ());
String imagefilename = "Img_" + TimeStamp + "_";
File ALBUMF = Getalbumdir ();
File IMAGEF = File.createtempfile (imagefilename, "JPG", ALBUMF);
return IMAGEF;
}
Private File Setupphotofile () throws IOException {
File f = createimagefile ();
Mcurrentphotopath = F.getabsolutepath ();
return F;
}
Private File Getalbumdir () {
File storagedir = null;
if (Environment.MEDIA_MOUNTED.equals (Environment.getexternalstoragestate ())) {
Storagedir = Malbumstoragedirfactory.getalbumstoragedir (Getalbumname ());
if (Storagedir!= null) {
if (! Storagedir.mkdirs ()) {
if (! storagedir.exists ()) {
LOG.D ("Camerasample", "Failed to create directory");
return null;
}
}
}
} else {
LOG.V (GetString (R.string.app_name), "External storage is not mounted read/write.");
}
return storagedir;
}