From: http://blog.csdn.net/zhaokaiqiang 1992/article/details/43022023
When making APP, if there is a user system function, it can't escape this requirement, that is, to set the user's avatar, and to set the avatar, which includes two ways: taking photos and selecting from the album. After the selection, the image is usually required to be tailored so that the user can set the avatar. Today's article is about how to fulfill this requirement.
Let's first analyze the requirements. For photography and selection from albums, you can send specific Intent to the system, call up the corresponding system program, and then in onActivity Result, get our data. There are two ways to tailor images. One is to process them by yourself, such as using third-party open source projects, such as Cropper.( https://github.com/edmodo/cropper To fulfill our needs, another way, we can directly use the cutting function provided by the system to achieve image clipping.
Before we implement the code, let's rationalize our thinking. If it's a photo taken from an album, in onActivity Result, we can get two forms of data, one is Bitmap, the other is uri. If the Bitmap object is too big, it may directly crash our program, so if all the pictures in the album are below 300px, the use of bitmap is allowed and safe, but it is basically impossible in our mobile phone. So I recommend using URI directly, no matter how big or small it is, because when you get uri, you get a picture pointer and do whatever you want.~
It's not too much of a problem to get pictures directly from the album, but if you want to use the pictures taken for processing, it may be a little more troublesome. After using Intent to call Photo APP, we can also get bitmap or URI in onActivity Result, depending on what flag we set in intent. However, if you get the Bitmap directly, it may not be the result you want to get. It may not return the original image, but the blurred thumbnail. This is the strategy Android system has made to reduce memory usage, but we can't use this thumbnail directly. So, when we get the picture from the photograph, we should also use uri. Type.
Well, in either way, we get the uri of the image we're dealing with, so what happens next? The uri is currently sent as data to Activeness for clipping using Intent. After clipping is completed, in on Activity Result, the clipped bitmap object is set to ImageView, then saved and uploaded to the server.
After understanding the whole process, we can start writing code.
There are two ways to wake up an album: Intent.ACTION_PICK and Intent.ACTION_GET_CONTENT. The code is as follows. Both ways can get uri data of the picture.
If we want to get it from a photograph, we can create a uri with a file object, then bind it together, so that when the photograph is successful, we can get the picture according to uri after returning, and save the result of the photograph in the file path. Here is the code implementation.
ok, what should we do when we get uri? Now it's time to open the tailoring interface again.~
The Intent that opens the tailoring interface is Intent intent = new Intent("com.android.camera.action.CROP");
For the meaning of these parameters in intent, please refer to the following figure
More important are the MediaStore.EXTRA_OUTPUT and return-data options.
If you set return-data to "true", you will get an Action associated with the internal data, and bitmap returns in this way: (Bitmap)extras.getParcelable("data"). Note: If the final image you want to get is very large, then this method will cause you trouble, so you need to control the output X and output Y to keep in a smaller size.
If you set return-data to "false", you will not receive any Bitmap in the Intent data of onActivityResult. Instead, you need to associate MediaStore.EXTRA_OUTPUT with a Uri, which is used to store Bitmap.
With that in mind, it should be clear to look at the following code.
A large part of the code above is dealing with multiple APPs that can accept tailored intent requests. If you only want to use the default first APP, you can delete the logic.
After that, we can do this in onActivity Result:
Wherever you choose, you need to go into the tailoring. After the tailoring is finished, we call setCropImg(), and set the result of the tailoring to ImageView, as follows
What is saveBitmap for? Of course, save the clipped Bitmap! Like the following
All right, now save it and upload it to the server. It's finished.~
Remember the weighting limit
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
Code download: https://github.com/ZhaoKaiQiang/CropImageDemo