A simple example of Android camera function _android

Source: Internet
Author: User
Tags create directory

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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.