Android calls the System camera to save and call the system album Method

Source: Internet
Author: User

If there are no new requirements for the system, direct calls are the most direct. The following describes how to call a system camera to take pictures and save images and call a system album. First, let's take a look at the core method of calling the System camera: Intent camera = new Intent (MediaStore. ACTION_IMAGE_CAPTURE); startActivityForResult (camera, 100); the data returned by the camera is obtained through the following callback method, and the copy code @ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resultCode, data); if (requestCode = CAMERA & resultCode = Activity. RESULT_ OK & null! = Data) {Bundle bundle = data. getExtras (); // get the data returned by the camera and convert it to the image format Bitmap bitmap = (Bitmap) bundle. get ("data") ;}} copy the code. The following describes how to call the system album and obtain the photo: Intent picture = new Intent (Intent. ACTION_PICK, android. provider. mediaStore. images. media. EXTERNAL_CONTENT_URI); startActivityForResult (picture, 101); or intent = new Intent (); intent. setType ("image/*"); intent. setAction (Intent. ACTION_GET_CONTENT); (Activity) context ). startAc TivityForResult (intent, 101); below is the corresponding callback method: copy the code @ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resultCode, data); if (requestCode = 101 & resultCode = Activity. RESULT_ OK & null! = Data) {Uri selectedImage = data. getData (); String [] filePathColumns = {MediaStore. images. media. DATA}; Cursor c = this. getContentResolver (). query (selectedImage, filePathColumns, null, null); c. moveToFirst (); int columnIndex = c. getColumnIndex (filePathColumns [0]); String picturePath = c. getString (columnIndex); c. close () ;}} copy the code or copy the code @ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {if (resultCode = Activity. RESULT_ OK) {switch (requestCode) {case 101: Uri uri = data. getData (); Cursor cursor = this. getContentResolver (). query (uri, null, null); cursor. moveToFirst (); String imgNo = cursor. getString (0); // Image number String imgPath = cursor. getString (1); // image file path String imgSize = cursor. getString (2); // image size String imgName = cursor. getString (3); // the image file name cursor. close (); Bitmap bitmap = BitmapFactory. decodeFile (imgPath); break; default: return ;}}}

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.