{{announcement.body}}
{{announcement.title}}

Data Transferring Between Android Based Mobile Devices

DZone 's Guide to

Data Transferring Between Android Based Mobile Devices

See how to further your understanding of socket communication between mobile devices with this practical example.

· Web Dev Zone ·
Free Resource

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. 

Java
 







Image or Video prompt


On activity result, we will get the selected file:

Java
 







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

Java
 







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.

Java


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

Java


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.

Java


File transferring in application


Show a message once the file is received.

Java


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

Java


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.

Topics:
android developers, android development, socket programming

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}