Android cameras and photo albums get pictures and save them to the SD card

Source: Internet
Author: User

For example, this requirement is not a complicated process, but there are some hidden risks. I recently encountered this problem in the project, I made a small research on Android's image acquisition through a camera or album and finally displayed on the interface. Now I will describe some results and the attached demo as follows:

Anyone who has done similar requirements knows that the camera can be started through the following code in the activity, and then the returned photo data can be obtained in the rewritten onactivityresult method:

Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(openCameraIntent, TAKE_PICTURE);

The problem arises. I wonder if you have found that the data obtained through the intent getdata method in the onactivityresult method is converted to bitmap and displayed on the interface. Sometimes the data cannot be obtained, or the bitmap displayed will be very small. If you save the bitmap to the SD card, you will find that the image resolution is very low and the image size is compressed, no matter how high the camera pixel is, finally, bitmap returned in this way is always compressed. What should I do if I want to get the desired Photo size and resolution? The following is my solution. Although it is not the best, it helps me solve this problem. Let me briefly explain why the returned image is compressed.

As we all know, the number of pixels on a mobile phone is 800 W or W, and the number is 4 kW (a sub-region). Take the photos taken from a common W pixel camera, the resolution is about 3200*2400. My testing model is LG Optimus 2X and 2.3.7, which is about pixels. The photo size is about 2 MB. Imagine that bitmap occupies 4 bytes in the Android system, which is 3200*2400*4 = ?, The results are calculated by yourself. It is unreasonable to consume such a large amount of memory for an image. In addition, the official documents indicate that, the maximum memory allocated to each application by the Android system is 16 Mb, the results returned from the images taken by the camera in the application are compressed, and the compressed images become small, the previously mentioned getdata method can only meet the needs of displaying an Avatar. If you want to display a large image, it will be blurred. So how can we get a clear big image? My solution is as follows:

1. When taking a photo, save the photo to a local device. The code before modification is as follows:

Uri imageuri = Uri. fromfile (new file (environment. getexternalstoragedirectory (), "image.jpg"); // specifies the photo retention period (sd, image.jpg) as a temporary file. After each photo is taken, the image will be replaced with opencameraintent. putextra (mediastore. extra_output, imageuri );

2. In the onactivityresult method, the image is taken out, scaled down, displayed on the interface, or uploaded to the server (custom compression ratio)

@ Overrideprotected void onactivityresult (INT requestcode, int resultcode, intent data) {super. onactivityresult (requestcode, resultcode, data); If (resultcode = result_ OK) {Switch (requestcode) {Case take_picture: // remove and zoom out the image stored locally and display it on the page. Bitmap = bitmapfactory. decodefile (environment. getexternalstoragedirectory () + "/image.jpg"); bitmap newbitmap = imagetools. zoombitmap (bitmap, bitmap. getwidth ()/scale, bitmap. getheight ()/scale); // because the bitmap memory usage is large, memory needs to be recycled here; otherwise, an out of Memory exception bitmap will be reported. recycle (); // display the processed image on the interface and save it to the local iv_image.setimagebitmap (newbitmap); imagetools. savephototosdcard (newbitmap, environment. getexternalstoragedirectory (). getabsolutepath (), String. valueof (system. currenttimemillis (); break; default: Break ;}}}

Because Android allocates a maximum of 8 MB of memory to bitmap, you can call the recycle () method to release the memory of a large bitmap. Then, the bitmap after the scale-out (which is included in the demo Source Code) is displayed on the interface or saved to the SD card. You can delete or store the previously saved source image, when you take a picture next time, the source image will be overwritten by the next image. Therefore, only one temporary image is used on the SD card, which is not very large.

The above section describes how to take a photo and get an image from the album. My method is as follows:

1. Open the album and select an image

Intent openAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT);openAlbumIntent.setType("image/*");startActivityForResult(openAlbumIntent, CHOOSE_PICTURE);

2. process the obtained image in the onactivity method. The idea is similar to the previous one.

@ Overrideprotected void onactivityresult (INT requestcode, int resultcode, intent data) {super. onactivityresult (requestcode, resultcode, data); If (resultcode = result_ OK) {Switch (requestcode) {Case choose_picture: contentresolver resolver = getcontentresolver (); // original resource address URI originaluri = data. getdata (); try {// use contentprovider to obtain the original image bitmap photo = mediastore through Uri. images. media. getbitmap (Resolver, Originaluri); If (photo! = NULL) {// to prevent memory overflow caused by the large size of the original image, first reduce the source Image Display and then release the memory occupied by the original bitmap. Bitmap smallbitmap = imagetools. zoombitmap (photo, photo. getwidth ()/scale, photo. getheight ()/scale); // release the memory occupied by the original image to prevent the out of Memory exception from photo. recycle (); iv_image.setimagebitmap (smallbitmap) ;}} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}break; default: Break ;}}}


Run (personal photo taken in summer ):

This article describes a problem found by a requirement. In combination with my solutions, we welcome students who have better methods to point out and communicate with each other!


Demo: Click to download

If you are interested in Android and iOS, you can join our QQ discussion group. Here, we will only discuss the following:

IOS group: 220223507

Android group: 282552849


If the article is useful to you, click the forward button in the lower right corner to share it with more people!

At the same time, you are welcome to follow my Sina Weibo chat with me: @ Tang Ren _ Ryan

Android comprehensive photo taking, photo album selection, and image cropping demo

Related Article

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.