Android calls system camera to take pictures and videos

Source: Internet
Author: User

This article implements the call of the Android system camera to take pictures

The layout of the project is fairly simple, with only one button:

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context=". Mainactivity " >    <ButtonAndroid:onclick= "click"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_centerhorizontal= "true"android:layout_centervertical= "true"Android:text= "Call system camera to take pictures" /></Relativelayout>

First open the manifest file under the Packages\apps\camera folder and locate the following code:

       <ActivityAndroid:name= "Com.android.camera.Camera"android:configchanges= "Orientation|keyboardhidden"Android:theme= "@android: Style/theme.black.notitlebar.fullscreen"android:screenorientation= "Landscape"Android:cleartaskonlaunch= "true"android:taskaffinity= "Android.task.camera">            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.DEFAULT" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter> <intent-filter><action  android:name= " Android.media.action.IMAGE_CAPTURE " /><category  android:name = "Android.intent.category.DEFAULT" /> </ Intent-filter >                                                                     <Intent-filter>                <ActionAndroid:name= "Android.media.action.STILL_IMAGE_CAMERA" />                <categoryAndroid:name= "Android.intent.category.DEFAULT" />            </Intent-filter>        </Activity>

The relevant code is as follows:

 Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }     Public voidClick (View view) {/** <intent-filter> <action * android:name= "Android.media.action.IMAGE_CAPTURE"/> <cate Gory * android:name= "Android.intent.category.DEFAULT"/> </intent-filter>*/        //activating the system's camera for taking picturesIntent Intent =NewIntent (); Intent.setaction ("Android.media.action.IMAGE_CAPTURE"); Intent.addcategory ("Android.intent.category.DEFAULT"); //save a photo to a specified pathFile File =NewFile ("/sdcard/image.jpg"); Uri URI=uri.fromfile (file);                Intent.putextra (Mediastore.extra_output, URI);    StartActivity (Intent); }}

It is also easy to implement the relevant code for activating the recording function:

 Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }     Public voidClick (View view) {/** <intent-filter> <action * android:name= "Android.media.action.VIDEO_CAPTURE"/> <cate Gory * android:name= "Android.intent.category.DEFAULT"/> </intent-filter>*/        //activating the system's camera for recordingIntent Intent =NewIntent (); Intent.setaction ("Android.media.action.VIDEO_CAPTURE"); Intent.addcategory ("Android.intent.category.DEFAULT"); //Save the recording to the specified pathFile File =NewFile ("/sdcard/video.3pg"); Uri URI=uri.fromfile (file);        Intent.putextra (Mediastore.extra_output, URI); Startactivityforresult (Intent,0); } @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {Toast.maketext ( This, "Call camera complete", 0). Show (); Super. Onactivityresult (Requestcode, ResultCode, data); }}

Android calls system camera to take pictures and videos

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.