
This is the last section of the photo sharing Android application tutorial. We will create the corresponding Java code and complete the whole application after changing some of the configuration values. We will then run the application and see the results correspondingly.
Open your strings.xml file and update the file with the below values make sure you update the Access key ID, access key secret, end point, bucket name, bucket endpoint and STS token.
• BreeSerif-Regular.ttf
• Bungee-Regular.ttf
Create a util package under the existing package and create the below mentioned Java files.
Let's create some utility classes for the application that is common for all the functionalities, such as providing customised font and application level constants.
This class contains provides the functionality of providing the typeface for the customized font.
Creating an object of this class requires the context parameter to be passed after initialization. Create the Java file as mentioned and update the code with the below provided:
This class contains all the static field members. These are the style definitions for various image processing functionalities in an application and their corresponding names. Create the file and update the code with the below provided:
Here we create the model classes for the application, which represents the information in a particular object
Create model package under the existing package at the same level of util package and create the below mentioned files:
This class represents the information required for the image processing feature of Alibaba Cloud. Our application will not be using all the attributes mentioned in this Bean class, but in other applications, we may use all the attributes. Create the corresponding file by copying the below code:
This class is created to transfer the file related information provided by camera, gallery, and documents to the OSS. Create the corresponding file and copy the code below
Now, we create multiple Java files which interacts with the XML files created in the previous section and provides the functionality for the same.
This Java file load the "welcome activity" XML and bootstrap the upload files and view images to the corresponding screens. You can even see that the fonts are being applied here. Create the file as named and copy the below code:
This is the adapter class for the recyclerview. This class functionality is to provide the input for the recyclerview and render the items in the recyclerview as requested by the Android system. Create the corresponding file and copy the below code:
This class is responsible for showing the list of images retrieved from Alibaba Cloud and updates the list when there is a change on the Alibaba Cloud OSS. This information will be shown when the user selects view images in the welcome activity. Create the file as mentioned and copy the code from below:
This class contains the complete functionality of the image processing feature. This class also invokes the camera, gallery, and documents folder. We will be using a third-party library called EasyImage. We will discuss more about main activity once we complete creating the files.
Copy below code into the activity:
The last and most important step after we creating the file is to register them in the AndroidManifest.xml. Update your manifest with the below code:
As you can see we require multiple permissions in the Android manifest, which includes internet, network state, Wi-Fi state, external storage, and camera. These permissions are required by the application for it to function normally.
Let's discuss about the logic involved in using the application. When a user clicks on upload images, a screen is launched. The welcome activity code is invoked, which then invokes the main activity code.
The top 3 image buttons, which display the camera gallery and document icon, provide the functionality of launching the default corresponding applications. After a user selects a file, the corresponding file will be uploaded on to the Alibaba Cloud. The user can see the same file by opening the Alibaba Cloud Object Storage Service (OSS) console.
We can use the image processing feature of OSS to perform image editing functionalities like resize, crop, rotate, and watermarking. The application uses a constant file to fit the image style definition and the corresponding image names. The application then invokes the image process task and make a request to the OSS service to transform the image. OSS returns the transformed image as a response to this request.
The processed image needs to be uploaded back onto the server from the client to be saved as an new image. You can also rewrite the existing image with the updated image, but personally, I prefer to keep all the transformed images separated. To do this, I send the original filename with the style we applied during the processing, and the application will save it as a new file using the putobjectrequest in the image process task.
Once all the images are processed, you can navigate to the welcome page to view the images. Recyclerview helps to display and provide the corresponding file names on the app.
Alibaba Cloud Object Storage Service (OSS) provides excellent response times upon uploading and downloading the images. I really like the asynchronous API provided by them, not many tools out there provide the same feature. It reduces the development time and the number of bugs we face when performing post development activities. There is a gentle learning curve for the beginners or developers working on it but in long-term it is very useful.
If everything is done correctly all your compilation issues will go away and the application starts installing in by clicking the small play (run) button in the status bar of the Android studio.