transfer files from android to computer via usb programmatically

12,478

Solution 1

I have solved this problem by using adb status-window to continuously check for device status, and when a new device is connected, the required files are transferred to the computer using adb pull command.

In order to achieve a portable solution (i.e. to run independently of Android platform), I just copied the files 'adb.exe' and 'AdbWinApi.dll' into my application and used the adb from there.

Solution 2

Had similar problem where a software installed in PC needed to be able to access file inside the android phone through USB. After long research here is what worked for me (not sure if this is the best solution but it worked). Windows has Windows Portable Device(WPD) API which can be used to enumerate contents of a device through USB (and copy files between client and device).

There are few implementations in java http://code.google.com/p/jmtp/ (works well but doesnt support copy function from device to pc though there is some support available to do this with source code) another one is jusbpmp (available in google repository) (if somebody finds a well documented and maintained implementation do share)

but since jmtp didnt work for me (copy function source compilation(for 64bit OS) failed), wrote C# program from scratch to copy files from device to PC. here is a nice tutorial https://github.com/geersch/WPD/blob/master/src/part-3/README.md

Share:
12,478
niculare
Author by

niculare

Enthusiast software developer.

Updated on July 26, 2022

Comments

  • niculare
    niculare almost 2 years

    I am looking for a solution which uses Android API to transfer a text file from an Android powered device to a computer through USB cable. I have found USB host but I cannot use this because the computer can not act as a device for the Android host.

    Do you have any suggestions how I can achieve this?

  • niculare
    niculare about 11 years
    The problem is that I can not rely on the wireless network connection. The only thing I can rely on is the USB cable.
  • Zonata
    Zonata almost 11 years
    Same here... I need to connect to a DB on a Windows PC through USB.
  • Juned
    Juned over 10 years
    After copying adb.exe and AdbWinApi.dll file what should i do ? running this Runtime.getRuntime().exec("adb -s pull /sdcard/juned.jpg /root/juned/android_usb/"); will be okay, or i need to do anything else ?