Android gets photos, cropped pictures and compressed pictures
preface
When doing the last project, I was deeply distressed by the image upload. Image upload is mainly divided into two parts. First, you need to obtain images, which can be obtained from files or photos. The second part is to upload pictures. Both parts have taken a lot of detours. Due to the serious fragmentation of the Android system, we may have the problem that we can get pictures on the first machine, but we can't get pictures on another machine, and we have to do some adaptation after Android 6.0 and 7.0, which is undoubtedly painful for developers. As a beginner, many things are not considered, and it is a little difficult to adapt.
In recent days, I also found a library from GitHub (the address is in takephoto). After simple learning, I found that it is quite simple to use, and can achieve the same effect between different models. More importantly, different effects can be achieved according to different configurations
Next, let's look at the usage
Get picture
1) Get takephoto object
1) By inheritance
Inherit one of takephotoactivity, takephotofragmentactivity and takephotofragment. Get the takephoto instance through gettakephoto() for related operations. Override the following method to get the result
Although this method is simple to use, it doesn't feel highly customizable. We must inherit the specified activity. Sometimes we have encapsulated baseactivity and don't want to change it. Sometimes inheritance cannot meet the needs of the actual project.
2) Use by assembly
Implement takephoto.takeresultlistener and invokelistener interfaces. In the onCreate, onActivityResult, onSaveInstanceState method, the method of TakePhoto pair is invoked. Rewrite onrequestpermissionsresult (int requestcode, string [] permissions, int [] grantresults) and add the following code.
Override the tpermissiontype invoke (invokeparam invokeparam) method and add the following code:
Add the following code to obtain the takephoto instance:
2) Customize UI
You can customize not only the parameters, but also the UI, such as custom photo album, custom toolbar, custom status bar, custom prompt text and custom cutting tool (you need to use the built-in takephoto to cut).
3) Get pictures from takephoto object
It supports obtaining from photo albums and taking photos, and related APIs
4) Crop configuration
Cropoptions is a configuration class for clipping. It can personalize the clipping scale, maximum output size, and whether to use the clipping tool of takephoto.
Compressed picture onenablecompress (compressconfig, Boolean showcompressdialog)
Specify the compression algorithm in the compression tool takephoto, which can also be compressed through the third-party Luban
Secondary encapsulation of takephoto
Encapsulation is the encapsulation of the second method, which mainly refers to the idea of the first method. The library code of takephoto is encapsulated in a takephotoutil tool class. See the code:
It encapsulates a basetakephotoactivity with the following code:
For other business encapsulation, another baseactivity can be encapsulated, which is inherited from basetakephotoactivity, so that the use of baseactivity will not be affected. If we use the function of obtaining pictures in the main activity, we need two steps
1) Turn on the takephoto function
2) get pictures
The first parameter of takephoto() is an enumeration type parameter, which is obtained from photo albums and photos respectively. The second parameter is to listen for success or failure. There are three callbacks. Because some callbacks are not necessary, an adaptation is made to the listener, which only needs to callback the desired method, After successful acquisition, you can obtain the desired picture and picture address through the parameters encapsulated by tresult. For the obtained picture address, you can do some upload processing.
Picture upload
The upload function can be realized with okhttp3
The approximate code is as above
last
Since we didn't find this library at that time, we went to ask another Android player in the company, looked at the code he encapsulated, and it was really worth learning. His code was also adapted to Android 7.0, and posted its code for future learning:
It also encapsulates the relevant functions obtained locally and photographed, which can be worth learning. After all, there are many pits.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.