In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces in detail "Android how to call the system camera to take pictures and cameras". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to call the system cameras to take pictures and take pictures in Android" can help you solve your doubts.
1. Invocation of existing camera applications in the system
As to how to call the existing application of the system, it has been explained before. Let's talk about it briefly here. To call an existing application of the system in the developed application, you need to use Intent to specify the Action and Category of the open application, and then open the specified Activity through startActivity (Intent) or startActivityForResult (Intent,int). If you use the startActivityForResult () method to open it and need to return a value, you can rewrite onActivityResult (int,int,Intent).
Let's first take a look at the Activity defined by the AndroidManifest.xml manifest file applied by the system's existing camera:
It defines two Activity,com.android.camera.Camera for cameras and com.android.camera.VideoCamera for cameras. As you can see from the literal meaning, in order to capture the data returned by the system camera, you generally need to use two Action to turn on the camera and camera:
The Action type of android.media.action.IMAGE_CAPTURE:Intent, which requests a picture from an existing camera application.
The Action type of android.media.action.VIDEO_CAPTURE:Intent that requests a video from an existing camera application.
The above two parameters are defined as static constants in the MediaStore class: MediaStore.ACTION_IMAGE_CAPTURE (camera) and MediaStore.ACTION_VIDEO_CAPTURE (camera).
2. The existing camera in the system takes photos.
As mentioned above, to open the existing camera application of the system to take photos, you need to use the MediaStore.ACTION_IMAGE_CAPTURE as the action of Intent to turn on Activity. However, when using the existing camera of the system, the picture will be saved to the directory of the system gallery by default. If you need to specify the save path of the picture file, you need to set it in Intent.
To set the save path of the photos taken by the existing camera in the system, you need to use the Intent.putExtra () method to set the additional data of Intent through MediaStore.EXTRA_OUTPUT. What is passed here is a Uri parameter, which can be the Uri of a file path.
Intent intent=new Intent (); / / specify the Action intent.setAction (MediaStore.ACTION_IMAGE_CAPTURE) to turn on the system camera; intent.addCategory (Intent.CATEGORY_DEFAULT); / / create a file File file=new File (FILE_PATH) based on the file address; / / convert the file address to Uri format Uri uri=Uri.fromFile (file) / / set the storage address of the image file intent.putExtra (MediaStore.EXTRA_OUTPUT, uri) after the photo is taken by the system camera.
3. Get the pictures taken by the existing cameras in the system.
In the newly opened Activity, if you need to obtain its return value, you need to use the startActivityForResult (Intent,int) method to open Activity, and rewrite onActivityResult (int,int,Intent) to obtain the return data of the system camera. Then we only need to get the return value in onActivityResult ().
The photos taken by the system camera, if you do not specify a path, will be saved in the system default folder, which can be obtained by using the Intent.getExtra () method. The result is a Uri address that indicates the address of a content provider. If you specify the save path through MediaStore.EXTRA_OUTPUT, you will get an empty address through Intent.getExtra (), but since it is the address we specified, you will not be afraid to find it.
4. The existing camera of the system takes pictures Demo.
The above explains how to use the system camera to take photos and get what it involves in the developed application, which is demonstrated by a simple Demo. In Demo, there are two Button to start the system camera in the way of specified path and no specified path, and get the return value to display in ImageView. The comments in Demo are more detailed, so I won't repeat them here.
Layout code: activity_syscamera.xml
Implementation code: SysCameraActivity.java
Package cn.bgxt.callsystemcamera;import java.io.File;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.provider.MediaStore;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.ImageView;public class SysCameraActivity extends Activity {private Button btn_StartCamera, btn_StartCameraInGallery; private ImageView iv_CameraImg; private static final String TAG = "main" Private static final String FILE_PATH = "/ sdcard/syscamera.jpg"; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_syscamera); btn_StartCamera = (Button) findViewById (R.id.btn_StartCamera); btn_StartCameraInGallery = (Button) findViewById (R.id.btn_StartCameraInGallery); iv_CameraImg = (ImageView) findViewById (R.id.iv_CameraImg); btn_StartCamera.setOnClickListener (click); btn_StartCameraInGallery.setOnClickListener (click) } private View.OnClickListener click = new View.OnClickListener () {@ Override public void onClick (View v) {Intent intent = null; switch (v.getId ()) {/ / specify the location to save photos taken by the camera: case R.id.btn_StartCamera: intent = new Intent (); / / specify the Action intent.setAction (MediaStore.ACTION_IMAGE_CAPTURE) that turns on the system camera; intent.addCategory (Intent.CATEGORY_DEFAULT) / / create a file based on the file address File file = new File (FILE_PATH); if (file.exists ()) {file.delete ();} / / convert the file address to Uri format Uri uri = Uri.fromFile (file); / / set the storage address of the image file intent.putExtra (MediaStore.EXTRA_OUTPUT, uri); startActivityForResult (intent, 0); break / / do not specify the address to save photos taken by the camera: case R.id.btn_StartCameraInGallery: intent = new Intent (); / / specify the Action intent.setAction (MediaStore.ACTION_IMAGE_CAPTURE) that turns on the system camera; intent.addCategory (Intent.CATEGORY_DEFAULT); startActivityForResult (intent, 1); break; default: break;} @ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {Log.i (TAG, "system camera completed, resultCode=" + resultCode); if (requestCode = = 0) {File file = new File (FILE_PATH); Uri uri = Uri.fromFile (file); iv_CameraImg.setImageURI (uri);} else if (requestCode = = 1) {Log.i (TAG, "default content address:" + data.getData ()); iv_CameraImg.setImageURI (data.getData ()) }}}
Effect display:
This is just a simple demonstration of how to call the existing camera application of the system to get the captured pictures without recycling the image resources, so there may be a memory overflow error, just restart the application.
5. The existing cameras in the system capture video.
The process of obtaining the captured video from the existing camera application of the system is roughly the same as the process of obtaining the captured image, but it can set other values besides setting the MediaStore.EXTRA_OUTPUT output path through putExtra (). Here is a brief introduction:
MediaStore.EXTRA_OUTPUT: sets the path where media files are saved.
MediaStore.EXTRA_VIDEO_QUALITY: sets the quality of video recording. 0 is low quality and 1 is high quality.
MediaStore.EXTRA_DURATION_LIMIT: sets the maximum allowed recording time for video (in milliseconds).
MediaStore.EXTRA_SIZE_LIMIT: specifies the maximum allowable size for video (in byte).
6. The existing camera of the system captures video Demo
Since it is the same as the process of taking photos, I will no longer repeat it here and go directly to Demo. Start an existing camera to capture video through a Button in Demo, and finally save it on the SD card.
Implementation code:
Package cn.bgxt.callsystemcamera;import java.io.File;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.provider.MediaStore;import android.util.Log;import android.view.View;import android.widget.Button;public class SysVideoCameraActivity extends Activity {private Button btn_StartVideoCamera; private static final String FILE_PATH = "/ sdcard/sysvideocamera.3gp"; private static final String TAG= "main"; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState) SetContentView (R.layout.activity_sysvideocamera); btn_StartVideoCamera = (Button) findViewById (R.id.btn_StartVideoCamera); btn_StartVideoCamera.setOnClickListener (click);} private View.OnClickListener click = new View.OnClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (); intent.setAction ("android.media.action.VIDEO_CAPTURE"); intent.addCategory ("android.intent.category.DEFAULT"); File file = new File (FILE_PATH) If (file.exists ()) {file.delete ();} Uri uri = Uri.fromFile (file); intent.putExtra (MediaStore.EXTRA_OUTPUT, uri); startActivityForResult (intent, 0);}}; @ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {Log.i (TAG, "resultCode=" + requestCode);}}
Effect display:
After reading this, the article "how to call the system camera to take photos and cameras with Android" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.