I have been researching this topic for days and I can't find anything on managing files on a MTP Portable Device (More specifically a Galaxy S4).
I want to be able to...
I really want to copy MP3 files but if there is a general way to copy over and file supported by MTP that would be awesome. I've looked into the Window Portable Device API but I couldn't find anywhere where there is sample code in C#.
Any blogs, sample code, and files would be very helpful. Thanks! :)
Media Transfer Protocol (MTP) is used for transferring files between devices. Notably, between newer Android or Microsoft smartphones and your Debian host. Be aware that many smartphones will only enable MTP while the phone is unlocked!
The Media Transfer Protocol (MTP) allows you to transfer files to and from your Windows 10 IoT Core device through USB. It allows access to the device's internal storage and the SD card, if present.
I used a nugetpackage called MediaDevices
this made it very easy for me to copy my photos from my android phone to my computer.
public class Program
{
static void Main(string[] args)
{
var devices = MediaDevice.GetDevices();
using (var device = devices.First(d => d.FriendlyName == "Galaxy Note8"))
{
device.Connect();
var photoDir = device.GetDirectoryInfo(@"\Phone\DCIM\Camera");
var files = photoDir.EnumerateFiles("*.*", SearchOption.AllDirectories);
foreach (var file in files)
{
MemoryStream memoryStream = new System.IO.MemoryStream();
device.DownloadFile(file.FullName, memoryStream);
memoryStream.Position = 0;
WriteSreamToDisk($@"D:\PHOTOS\{file.Name}", memoryStream);
}
device.Disconnect();
}
}
static void WriteSreamToDisk(string filePath, MemoryStream memoryStream)
{
using (FileStream file = new FileStream(filePath, FileMode.Create, System.IO.FileAccess.Write))
{
byte[] bytes = new byte[memoryStream.Length];
memoryStream.Read(bytes, 0, (int)memoryStream.Length);
file.Write(bytes, 0, bytes.Length);
memoryStream.Close();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With