The Best Way to Develop an Android Application to Share Files and Photos with Friends

ibrahimertanylmz
Huawei Developers
Published in
6 min readMar 8, 2023
Music Video Picture Frame GIF By Radical Face

Introduction

Hi everyone 👋

This article is based on Using Cloud DB to Share Files and Photos with Friends feature of Photography App. Photography App is an application that allows you tagging and editing photos, showing the location that photo was taken, adding friends, sharing files and photos with your friends.

This feature of Photography App helps us to share file with a group of people and inside that file sharing photos with them and also giving them opportunity to share photos.

Also you can reach out the codelab from here. Let’s get started!

Contents

Integration & Initialization of Cloud DB

Share a File with Friends

Share Photos with Friends in the Shared File

Shared Files with Me: Updating Listening in Real Time

Conclusion

References

Integration & Initialization of Cloud DB

Before using the Cloud DB service, you need to enable it. You can follow the steps from Enabling the Service. Then you need to create object types on the AppGallery Connect console and export the object type file in the Java format for Android application development. You can follow the steps on Adding and Exporting Object Types to add PhotoDetails object. Add the fields and select id as primary key as the table below and export the JAVA file.

PhotoDetails object table

Next, create a CloudDb Zone by following the steps on Adding a Cloud DB Zone. Then, add the exported JAVA file to the local development environment. If the file already exists, replace it with the exported one. To ensure successful integration, do not modify the exported JAVA file.

JAVA file model path

After adding the object type file, you can develop your app using Cloud DB. Before that, you need to initialize AGConnectCloudDB, and create a Cloud DB zone and an object type. You can check the sample code under each of the following steps:

1. Initialize Cloud DB in your application class. For this, create an initialize method in PhotoApplication.kt to initialize AGConnectCloudDB.

2. Create RoutePolicy, Instance, and ObjectTypes for Cloud DB. In this case a CloudDBModule used with singleton principle on our project.

3. Create the Cloud DB zone configuration object, and open the Cloud DB zone.

Also you can see the writing data to Cloud DB from the documentation.

Share a File with Friends

In this section, we need to use PhotoDetails object to share files with our friends. If you are curious about add friend feature of Photography App you can check out from this article. If the id’s of users would be added as a recieverId to our table, then the user can reach out that file in the Shared Files with You section. Also the sender can see the files in the Files You Shared section. Let’s go step by step to make it simpler.

1. After entering the file information, click the save button. In the listener of the save button, create fileInformation variable. Use setFragmentResult() function to reach fileInformation data in the ShareImageFragment.kt. And call controlFileInformationModel() to check the values. If all values are not empty, back to the ShareImageFragment.kt and start the sharing process there.

Add File Information Photography App

2. Meanwhile, in the ShareImageFragment.kt, get the fileInformation data using setFragmentResultListener(). Call the prepareFileData() function to convert fileInformation to PhotoDetails object.

3. With the prepareFileData() function, we share the file with selected users. Therefore, we save the file informations that is converted to PhotoDetail object with saveToCloudDB() function in Writing Data to Cloud DB part.

Share Photos with Friends in the Shared File

In this part, inside PhotoDetails object we need to share photos using that file to the user’s selected by the sender. So the images will be shared in that file should have a connection with that file’s Id. Let’s look at this section with details.

1. Click “Select Photo” button to open gallery. In the listener function, launch selectPhotosWithIntent and call registerForActivityResult() function. When we get the result, we call the setSelectedPhoto() function.

2. We convert our photos that come in the List<Uri> type into mutableListOf<Bitmap> list to share. And also convert arrayListOf<Photos> type to show images in recyclerView.

3. When we clicked the “Share” button, we call the sharePhotos() function. In this function, we use selectedPhotos list to create Photos() models and save the photos to Cloud DB.

Here is the example result for sharing photos with friends in the shared file:

Share Photos with Friends Gif

Shared Files with Me: Updating Listening in Real Time

In the process of file sharing, there are processes for receiving and viewing both the files we shared and the files shared with us from the cloud. We will go through the example of the files shared with us in this section, similar steps will be applied to the files we shared.

1. The first function that starts this process is the observeCloudDbZone() function. This function starts the subscription process to listen for updates in the cloud. We use the “senderId” parameter for the files we share, and the “receiverId” parameter for the files shared with us.

2. subscribeSnapshot() method is used to listen on data changes that meet the specified query conditions. In this example, we should subscribe to listen updating of shared files with me in real time. Therefore, call the addSubscriptionForSharedFilesWithYou() with cloudDBZoneInstance, queryTitle (like “receiverId”), queryValue (like “agConnectUser.currentUser.uid”) parameters as you can see before step.

3. To take the result, create a listener named mSnapshotListenerForSharedFilesWithYou. When data on the cloud changes, this snapshot listener is called immediately and a snapshot is generated so that you can obtain data change information through the snapshot.

4. Finally, the result should be observed, which comes from the snapshot listener and updates the list for RecyclerView.

As a result for “Sharing Files and Updating Listening in Real Time” you can check the gif below:

Shared Files Gif

Conclusion

The article explained Share Files and Photos with Friends feature from the Photography App using Cloud DB. This guide includes “Sharing File with Friends”, “Sharing Photos with Friends in the Shared File” and “Shared Files with Me: Updating Listening in Real Time”. The article helps you to understand sharing photos with multiple people using a file and also the other people can add pictures to the same file and can use it as a online folder. Also updating data in real-time is possible if you follow the article.

I hope, you found this article helpful and if you have any comments or questions, please let me know in the comments below.

You can also check out other articles for different features of Photography App from below:

--

--

ibrahimertanylmz
Huawei Developers

Android Developer @Huawei 💻, ESOGU Computer Engineering Graduate 🎓, Proactive Self-Starter, Quick Learner, Team Player 👨‍👦‍👦