Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the File path to specify to transfer a file from Android to Windows PC?

Tags:

c#

android

mtp

wpd

I am going to create an c# windows application for transfering image file from an android phone to my Wondows PC - when I connect phone with my PC(using data cable). When I given the path "Computer/Nuxes5/..." in C# for accessing files from mobile, (Got from the windows Explorer address bar). Then getting incorrect path. Following is the code I have given for accessing files.

Directory.GetFiles(@"Computer/Nuxes5/...");

Can any one please suggest me, how to access the mobile files using C#.

like image 331
Sajith A.K. Avatar asked Nov 10 '22 15:11

Sajith A.K.


1 Answers

You need to use MTP file transfer. Since you are using Windows, the best thing to do is to use COM with the Windows PortableDeviceApiLib library. This is not an easy task. The WPD API link in one of the comments above is a good reference.

You should also install Microsoft MTP Simulator 3.0 and look at the sample code that comes with it.

In MTP, every file or folder stored on the device is an object with a handle. To retrieve a file or a folder, you have to retrieve the object handle, then check to see if it is a file or a folder by checking its objectFormatCode property. Folders have the object format code set to 0x3001. You can get the entire list from the MTP Spec.

Once you have the WPD/PTP wrapper set up, you can start sending MTP commands to the device. For getting files from the device, the procedure is the following.

  1. Get the available storage ids by calling getStorageIds();
  2. For the storage id you are interested in (internal storage/SD card), call getObjectHandles() to get a list of all files/folders.
  3. Loop through the root folder handle to look for the file you are interested in. For each handle you get, call getObjectInfo() to get the details about that handle.
  4. Once you have a handle whose name and format code matches what you are looking for, call the getObject() function to download the file.

Also remember that you cannot download all the contents at the same time. You have to call getObject() for each file handle you need to download.

like image 80
avian Avatar answered Nov 14 '22 21:11

avian