In my previous article, I had shared some concepts of socket programming and gone through how to connect two Android devices using socket communication. I suggest that you read that article before you start.

In this part, we will learn how to send data between two devices once they are connected.

First, I’ll show a dialog box, asking for the user to choose between an image and video. Then, based on our user’s choice, we will use the MediaPicker library for file selection. 

Image or Video prompt

On activity result, we will get the selected file:

SendFileModel is a model where we can manage file type, send, receive status, and list all of our files.

To send a list of files, you need to get the total size of the file and send the size to the receiver. Then, you need to write the file name and the directory name if you want the same directory as per the sender. After that, start writing the content of the file.

At the receiving end, we need to check the file type, and if the type is file, start reading the data.

First, get the total size, file size, and file name, and create the folder, as per your need. THen, you can start reading files, while sending and receive the files show progress bar to show file is being send.

File transferring in application

Show a message once the file is received.

Once the file is sent, reset the list in the application class and application class manage list of the file. 

After that, the file can be seen in the newly created folder. This way, you can also show the list of files received, and before sending, show all the files in the list view.

Demo:

Demo of application

Source Code: https://github.com/omeryilmaz86/AndroidDataSharing.git.



Source link

Write A Comment