This question definitely help me! I was trying to write a simple program to auto sync my photos/videos from an android device to my computer. Hence I ended up asking the same question as Herbert Yu, how do I access the device path when there are no drive letters?
ZackOfAllTrades' answer helped me. Thumbs up!
I copied ZackOfAllTrades' code, tweaked it to my needs and Bang! it worked, but not for long. I have a 108MP camera on my Xiaomi Note 10 Pro, 4 minutes videos can easily go past 1GB; some of my videos are 4GBs large. using ZackOfAllTrades' code, I quickly ran into OutOfMemoryException when invoking MediaDevice.DownloadFile(string, Stream).
[1] My initial attempt at fixing this is to go to project properties, set my project to build for x64, that seems to get rid of OutOfMemoryException; I am able to start copying files between 1GB to 2GB without any problems.
[2] However, as soon as I start copying files of 2.5GB large, the WriteStreamToDisk() util function written by ZackOfAllTrades started to complain about Stream too long.
Then I realized that DownloadFile takes a Stream object, it doesn't need to be a MemoryStream as Zack has used. So I switched it to a FileStream object as follow
static void Main(string[] args)
{
string DeviceNameAsSeenInMyComputer = "Mi Note 10 Pro";
var devices = MediaDevice.GetDevices();
using (var device = devices.Where(d => d.FriendlyName == DeviceNameAsSeenInMyComputer || d.Description == DeviceNameAsSeenInMyComputer).First())
{
device.Connect();
var photoDir = device.GetDirectoryInfo(@"\Internal shared storage\DCIM\Camera");
var files = photoDir.EnumerateFiles("*.*", SearchOption.TopDirectoryOnly);
foreach (var file in files)
{
string destinationFileName = $@"F:\Photo\{file.Name}";
if (!File.Exists(destinationFileName))
{
using (FileStream fs = new FileStream(destinationFileName, FileMode.Create, System.IO.FileAccess.Write))
{
device.DownloadFile(file.FullName, fs);
}
}
}
device.Disconnect();
}
Console.WriteLine("Done...");
Console.ReadLine();
}
Worked beautifully. When I am using ZackOfAllTrades' code, VS profiler shows memory consumption at about 2.5 times the size of file.
Eg: if the file is 1.5GB large, the memory consumption is roughly 4GB.
But if one is to copy to file system directly, the memory consumption is negligible (<50mb)
The other issues is with MediaDevice.FriendlyName. In ZackOfAllTrades' example, he is using a Samsung, and I am guessing Samsung phones supported this property. I did not bother digging out my old Samsung to try this out. I know for sure my Xiaomi Note 10 Pro did not support FriendlyName, instead what worked for me is MediaDevice.Description
Hope this helps others who are asking the same question.