Recently, when I was reconstructing the picture module of APP, I incidentally wrote this picture operation library, which basically covers all picture operations in APP development.

The image gallery contains features

  • Secondary packaging of image loading library;
  • Photo album features
  • Select photos from album/camera and crop them;
  • Image compression;
  • Upload pictures to the server;
  • Upload pictures to Qiniu;

Module is introduced

Secondary packaging of the image loading library

Uniform packaging for Fresco, ImageLoader, Glide, and also extensible. See ImageLoaderManager for details

Photo album features

Similar to the circle of friends to select photos, provides a simple interface, can quickly realize the function of selecting multiple photos;

New photopicker.builder ().setisDialog (false).setisShowCamera (false).setMaxPhotoCount(3) .setSelectedPhotos(selectedPhotos) .builder().chooseImage(AlbumActivity.this); @override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PhotoPicker.REQUEST_CODE_CHOOSE_PHOTO && data ! = null) { selectedPhotos = data.getStringArrayListExtra(PhotoPicker.SELECTED_PHOTOS); if (selectedPhotos ! = null && selectedPhotos.size() > 0) {// Handle logic}}}Copy the code

PS: The processing logic here refers to PhotoPicker

Select photos from album/camera

This may be an indispensable small function of the APP, but there are also many compatibility pits. In order to facilitate future use, it is packaged into a simple tool class, which can realize the operation of changing the avatar through a few lines of simple code.

/ / should choose photos after cutting ChoosePhotoManager getInstance () setAutoCrop (true); / / to get photos from camera ChoosePhotoManager. GetInstance () choosePhotoFromCamera (ChooseAndCropImageActivity. This); / / select photos from albums ChoosePhotoManager. GetInstance () choosePhotoFromAlbum (ChooseAndCropImageActivity. This); / / listen to choose photos ChoosePhotoManager. The result of the getInstance (). SetChoosePhotoListener (new OnChoosePhotoListener () {@ Override public void ChoosePhotoFromAlbum (Uri Uri, String errMsg) {Override public void choosePhotoFromCamera(Uri Uri, String errMsg) {Override public void choosePhotoFromCamera(Uri Uri, String errMsg) String errMsg) {Override public void cropPhoto(Uri Uri, String errMsg) {Override public void cropPhoto(Uri Uri, String errMsg) {Copy the code

Image compression

In order to better user experience and reduce the storage pressure of the server, we should carry out appropriate compression before uploading pictures. Here we use Luban library, which can be compressed before uploading pictures through simple calls.

void compressImage(Context context, String filePath, OnCompressListener listener)
Copy the code

Upload images to the server

Since photos are selected, uploads are definitely required. To reduce the dependency on other open source libraries, uploads are implemented using HttpURLConnection (OKHttp is easier, of course).

UploadToInnerServer (Activity Context, String serverAddress, String filePath, final OnUploadListener listener)Copy the code

Upload pictures to Seven Cows

There are two ways to upload pictures, one is to upload to the company server, and then from the company server to the picture server, and the other is directly from the client to the picture server. Uploading to Qiniu is the second solution: first obtain the token from the company’s server and then upload the picture to Qiniu’s server.

void uploadToQiNiu(Activity context, String token, String filePath, OnUploadToQiNiuListener listener)
Copy the code

The project address

ImageSet

This library basically encapsulates all the operations of images in the APP. If you have any questions in the process of use, please welcome to issue.