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