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 camera to take pictures in Android

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to use the camera to take pictures in Android". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to call the camera in Android" can help you solve the problem.

The first step is to get permission

If (ContextCompat.checkSelfPermission (this, Manifest.permission.WRITE_EXTERNAL_STORAGE)! = PackageManager.PERMISSION_GRANTED) {/ / if you don't have permission, dynamically apply for authorization ActivityCompat.requestPermissions (this, new String [] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2);}

The second step is to create a file for saving and call the system camera camera program with intent

Button btTakePhoto = (Button) findViewById (R.id.btTakePhoto); btTakePhoto.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {try {File sdcard = Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES); picFile = new File (sdcard, System.currentTimeMillis () + ".jpg"); picFile.createNewFile () Log.e ("UseSystemCameraApp", picFile.getName () + "created successfully");} catch (IOException e) {e.printStackTrace ();} Intent intent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra (MediaStore.EXTRA_OUTPUT, Uri.fromFile (picFile)) StartActivityForResult (intent, 1);});}

Step three, show the picture.

Protected void onActivityResult (int requestCode, int resultCode, Intent data) {if (requestCode = = 1) {ImageView iv = (ImageView) findViewById (R.id.ivShow); iv.setImageURI (Uri.fromFile (picFile));} super.onActivityResult (requestCode, resultCode, data);}

OnRequestPermissionsResult is also needed.

@ Override public void onRequestPermissionsResult (int requestCode, @ NonNull String [] permissions, @ NonNull int [] grantResults) {super.onRequestPermissionsResult (requestCode, permissions, grantResults) If (requestCode = = 1) {if (! (grantResults.length > 0 & & grantResults [0] = = PackageManager.PERMISSION_GRANTED)) {Toast.makeText (this, "not granted SD card access", Toast.LENGTH_LONG) .show (); finish ();}

It seems that the steps to be done have been completed at this point, but there is actually a final step, because we are using intent.putExtra, which will cause the URI of the current activity to be exposed to the next activity, and if not handled, an error will be reported. So StrictMode.VmPolicy.Builder should be used for monitoring in onCreate.

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder (); StrictMode.setVmPolicy (builder.build ()); builder.detectFileUriExposure (); Button btTakePhoto = (Button) findViewById (R.id.btTakePhoto). That's all for "how to use the camera in Android". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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