Use Android built-in camera for photo taking

Source: Internet
Author: User
Main Program:

Public class androcamera extends activity {

Private Static final int image_capture = 0;

Private button startbtn;

Private URI imageuri;

Private imageview;

/** Called when the activity is first created.

* Sets the content and gets the references

* The basic widgets on the screen like

* {@ Code button} or {@ link imageview}

*/

@ Override

Public void oncreate (bundle savedinstancestate ){

Super. oncreate (savedinstancestate );

Setcontentview (R. layout. Main );

Imageview = (imageview) findviewbyid (R. Id. IMG );

Startbtn = (button) findviewbyid (R. Id. startbtn );

Startbtn. setonclicklistener (New View. onclicklistener (){

Public void onclick (view v ){

Startcamera ();

}

});

}

Public void startcamera (){

Log. D ("andro_camera", "Starting camera on the phone ...");

String filename = "testphoto.jpg ";

Contentvalues values = new contentvalues ();

Values. Put (mediastore. Images. Media. Title, filename );

Values. Put (mediastore. Images. Media. description,

"Image capture by camera ");

Values. Put (mediastore. Images. Media. mime_type, "image/JPEG ");

Imageuri = getcontentresolver (). insert (

Mediastore. Images. Media. external_content_uri, values );

Intent intent = new intent (mediastore. action_image_capture );

Intent. putextra (mediastore. extra_output, imageuri );

Intent. putextra (mediastore. extra_video_quality, 1 );

Startactivityforresult (intent, image_capture );

}

Protected void onactivityresult (INT requestcode, int resultcode, intent data ){

If (requestcode = image_capture ){

If (resultcode = result_ OK ){

Log. D ("andro_camera", "picture taken !!! ");

Imageview. setimageuri (imageuri );

}

}

}

Page file:

<? XML version = "1.0" encoding = "UTF-8"?>

<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: Orientation = "vertical"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

>

<Button Android: text = "Start camera"

Android: Id = "@ + ID/startbtn"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content">

</Button>

<Imageview Android: Id = "@ + ID/IMG"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent">

</Imageview>

</Linearlayout

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.