Android image upload to achieve preview effect
First, analyze the functions to be realized, in which requirement analysis is essential. After the requirements and logic are cleared, and then write the handwritten code, the idea will be very clear.
1. To upload multiple pictures, you must first select pictures (the project requirement here is that you can take photos and upload them or select from photo albums)
2. It's easy to upload photos. There are many examples on the Internet. Call the camera, return URI and get pictures
3. Select a picture from the album
The main logic is basically like this. Let's take a look at the implementation below:
1、 First take a look at the interface:
It is a noscrollgridview, which is required by the project, so the GridView without scrolling is used. The same is true for everyone using GridView.
This is an adapter for initializing the image array (I use an adapter for new images, failed upload images, and successfully uploaded images)
Imageitem is a model of an image, and its properties are shown below
Next comes the adapter:
As it involves adding pictures, a flag is added in the adapter to display the newly created pictures, the selected pictures are added to the public picture array, and the picture array is loaded for display during initialization. (you can ignore my flag when watching)
The following is the user-defined album interface, which is used to select pictures
GridView is also used to obtain the pictures in the mobile phone, add them to the array and display them
Here we use an album helper (photo album help class), which is the code found on the Internet. I feel very good
After the data source is obtained, it will be displayed, that is, the methods in the image acquisition help class will be used
Click image selection to add it to the public image array to display the selected image.
Finally, preview the picture. Use the custom viewpager to slide the picture. Here are two screenshots:
The above is the whole content of this article. I hope it will be helpful to your study.