Android hardware learning-camera learning Android can't help but learn cameras. Now all kinds of beauty cameras and Wechat moments are shown. Now, after upgrading, you can pull down and take photos,

Source: Internet
Author: User

Android hardware learning-camera learning Android can't help but learn cameras. Now there are various kinds of beauty cameras and pictures in the circle of friends. Now, after the upgrade, you can pull down and take pictures,
I can't help learning about Android without learning cameras. Now all kinds of beauty cameras and pictures are shown in the circle of friends. Now, after the upgrade, I can pull down and take photos. The source of all kinds of photos is also a camera. It's a little evil, let's take a look at the usage of cameras in Android. In Android, cameras are generally divided into two types: using an existing Camera App (which can be understood as a built-in camera in the system) and creating a camera App separately, the first method is easy to understand, and the second method can support a company if it is good ~ Today, we will briefly describe the first call ~ For basic work, cameras need to be photographed, and photos need to be stored. permissions need to be set for external storage. Three permissions set for this operation: one is to call the Camera permission, the second is to require the device to have a camera, and the third is to write permissions on the external device: <uses-permission android: name = "android. permission. CAMERA "/> <uses-feature android: name =" android. hardware. camera "/> <uses-permission android: name =" android. permission. WRITE_EXTERNAL_STORAGE "/> simple page: A Buttom, an ImageView: Demo implements a simple trilogy. First, create an Intent, start the Intent, and finally receive the result without the front camera, it is simulated: Photo event: public void cameraEvent (View view) {intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE); // create an intent to obtain the image file = getOutputMediaFile (); // obtain the path intent. putExtra (MediaStore. EXTRA_OUTPUT, Uri. fromFile (file); // set the File name // start Intent startActivityForResult (intent, IMAGE_REQUEST_CODE);} to obtain the file path: // The image path is private static File getOutputMediaFile () {File mediaStorageDir = new File (Environment. getExternalStoragePublicDirectory (Enviro Nment. DIRECTORY_PICTURES), tag); if (! MediaStorageDir. exists () {if (! MediaStorageDir. mkdirs () {Log. d (tag, "Storage Directory creation failed"); return null ;}// create a file name String timeStamp = new SimpleDateFormat ("yyyyMMdd_HHmmss "). format (new Date (); File mediaFile = null; mediaFile = new File (mediaStorageDir. getPath () + File. separator + "IMG _" + timeStamp + ". jpg "); Log. d (tag, "storage directory:" + mediaFile); return mediaFile;} The onActivityResult method in the Activity must be rewritten to receive events after completion: @ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stub if (requestCode = IMAGE_REQUEST_CODE & resultCode = RESULT_ OK) {imageView = (ImageView) findViewById (R. id. camera_img); // you can use this Bitmap bitmap = BitmapFactory when the image size is large. decodeFile (file. toString (); imageView. setImageBitmap (bitmap); // imageView. setImageURI (Uri. fromFile (file);} 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.