Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to call the existing camera application of the device by Android

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how Android invokes the existing camera applications of the device. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

1. Photo 1.1 request camera function

Add to the manifest file:

...

If your application uses a camera but does not need a camera to function properly, set android:required to false. In this way, Google Play allows devices that do not have a camera to download your application.

Call camera app to get thumbnails static final int REQUEST_IMAGE_CAPTURE = 1; / call camera app to take photos private void takePictureIntent () {Intent takePictureIntent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity (getPackageManager ())! = null) {startActivityForResult (takePictureIntent, REQUEST_IMAGE_CAPTURE) Call the camera app to get the complete picture / / call the camera app to get the complete picture private void takePictureGetFile () {Intent takePictureIntent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity (getPackageManager ())! = null) {File photoFile = null; try {photoFile = createImageFile () } catch (IOException ex) {ex.printStackTrace ();} if (photoFile! = null) {Uri photoURI = FileProvider.getUriForFile (this, this.getPackageName () + ".fileprovider", photoFile); takePictureIntent.putExtra (MediaStore.EXTRA_OUTPUT, photoURI); startActivityForResult (takePictureIntent, REQ_2) }} 1.4 camera application returns thumbnail image, complete picture

The Android camera application encodes the photos in the returned Intent (passed to onActivityResult () as a small Bitmap in extra, using the key "data").

The following code retrieves the picture and displays it in an ImageView

@ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {super.onActivityResult (requestCode, resultCode, data); / / get thumbnail if (requestCode = = REQ_1 & & resultCode = = RESULT_OK) {Bundle extras = data.getExtras (); Bitmap imageBitmap = (Bitmap) extras.get ("data"); ivThumbnail.setImageBitmap (imageBitmap) } / / get the complete picture if (requestCode = = REQ_2) {FileInputStream fis = null; try {fis = new FileInputStream (currentPhotoPath); Bitmap bitmap = BitmapFactory.decodeStream (fis); ivComplete.setImageBitmap (bitmap);} catch (FileNotFoundException e) {e.printStackTrace () } finally {try {fis.close ();} catch (IOException e) {e.printStackTrace ();} / / watch video if (requestCode = = REQ_3 & & resultCode = = RESULT_OK) {Uri videoUri = data.getData (); vvVideo.setMediaController (new MediaController (this)) VvVideo.setVideoURI (videoUri); vvVideo.start ();}} 2. Save the file configuration

Having write permission implies that it can be read, so you only need to request write permission

...

Note:

GetUriForFile (Context, String, File), which returns content:// URI. For the latest applications targeting Android 7.0 (API level 24) and later, passing file:// URI across package boundaries results in FileUriExposedException. Therefore, there is a more general way to use FileProvider to store pictures.

Add a provider to the manifest file:

......

Make sure the authorization string matches the second parameter of getUriForFile (Context, String, File). Create a new file res/xml/file_paths.xml

The path component corresponds to the path returned by getExternalFilesDir () (when Environment.DIRECTORY_PICTURES is called). Be sure to replace com.example.package.name with the actual package name of the application.

3. Record video 3.1 call camera application to record video / / call camera application to record video private void takeVideo () {Intent takeVideoIntent = new Intent (MediaStore.ACTION_VIDEO_CAPTURE); if (takeVideoIntent.resolveActivity (getPackageManager ())! = null) {/ / if it is null, the application will crash File videoFile = null; try {videoFile = createVideoFile () } catch (IOException ex) {ex.printStackTrace ();} if (videoFile! = null) {Uri videoURI = FileProvider.getUriForFile (this, this.getPackageName () + ".fileprovider", videoFile); takeVideoIntent.putExtra (MediaStore.EXTRA_OUTPUT, videoURI); startActivityForResult (takeVideoIntent, REQ_3) Camera App returns video @ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {super.onActivityResult (requestCode, resultCode, data); / / watching video if (requestCode = = REQ_3 & & resultCode = = RESULT_OK) {Uri videoUri = data.getData (); vvVideo.setMediaController (new MediaController (this)); vvVideo.setVideoURI (videoUri); vvVideo.start () }} this is the end of the article on "how Android invokes existing camera applications on devices". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report