Android calls the System camera to take pictures and videos, android camera

Source: Internet
Author: User

Android calls the System camera to take pictures and videos, android camera

This document calls the android system camera to take photos.

The project layout is quite simple, with only one Button:

<RelativeLayout xmlns: 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 "> <Button android: onClick =" click "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_centerHorizontal =" true "android: layout_centerVertical = "true" android: text = "Call the System camera to take a photo"/> </RelativeLayout>

First, open the configuration file under the packages \ apps \ Camera folder and find the following code:

       <activity android: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>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.DEFAULT" />                <category android: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>                <action android:name="android.media.action.STILL_IMAGE_CAMERA" />                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>        </activity>

The related code is as follows:

Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void click (View view) {/** <intent-filter> <action * android: name = "android. media. action. IMAGE_CAPTURE "/> <category * android: name =" android. intent. category. DEFAULT "/> </intent-filter> * // activate the system camera to take a photo. Intent intent = new Intent (); intent. setAction ("android. media. action. IMAGE_CAPTURE "); intent. addCategory ("android. intent. category. DEFAULT "); // Save the photo to the specified path File file = new File ("/sdcard/image.jpg "); Uri uri Uri = Uri. fromFile (file); intent. putExtra (MediaStore. EXTRA_OUTPUT, uri); startActivity (intent );}}

The code for activating the video function is also very simple:

Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void click (View view) {/** <intent-filter> <action * android: name = "android. media. action. VIDEO_CAPTURE "/> <category * android: name =" android. intent. category. DEFAULT "/> </intent-filter> * // enable the system camera to video Intent intent = new Intent (); intent. setAction ("android. media. action. VIDEO_CAPTURE "); intent. addCategory ("android. intent. category. DEFAULT "); // Save the video to the specified path File = new file ("/sdcard/video.3pg "); Uri uri Uri = Uri. fromFile (file); intent. putExtra (MediaStore. EXTRA_OUTPUT, uri); startActivityForResult (intent, 0) ;}@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {Toast. makeText (this, "camera called", 0 ). show (); super. onActivityResult (requestCode, resultCode, data );}}

 

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.