如何从手机下载文件? MediaDevices device.DownloadFile()方法引发Not Implmented异常

时间:2020-08-07 01:30:51

标签: mediadevices

我正在尝试使用MediaDevices库从手机下载文件。我可以在手机上看到文件,但是MediaDevice.DownloadFile()抛出NotImplemented异常。我有该库的1.8.0版本。这是我的程序:

using System;
using System.Diagnostics;
using System.IO;
using MediaDevices;

namespace ReadPhoneFile
{
    class Program
    {
        static MediaDevice thePhone;
        
        static void Main(string[] args)
        {
            var devices = MediaDevice.GetDevices();
            foreach (MediaDevice device in devices)
            {
                try
                {
                    thePhone = device;
                    device.Connect();
                    DownloadFiles(device, "e:/pictures/Rob's Test Phone/Lightroom");
                    device.Disconnect();
                }
                catch
                {
                    // If it can't be read, don't worry.
                }
            }
            Console.WriteLine("Press any key.");
            Console.ReadKey();
        }

        static void DownloadFiles(MediaDevice device, string pcFolder)
        {
            var lightroomDir = device.GetDirectoryInfo(@"Phone\Android\data\com.adobe.lrmobile\files\carouselDocuments\a6f0275ee0b94c90b3c05f093250d4ef\Originals");
            // ProcessLightroomFolder(photoDir, pcFolder, false);
            foreach (MediaDirectoryInfo subFolder in lightroomDir.EnumerateDirectories())
            {
                ProcessLightroomFolder(subFolder, pcFolder, true);
            }
        }

        static void ProcessLightroomFolder(MediaDirectoryInfo folder, string pcFolder, bool createFolder)
        {
            Console.WriteLine($"Processing folder {folder.Name} into folder {pcFolder}");
            string pcSubFolder = $"{pcFolder}/{folder.Name}";
            if (createFolder)
            {
                Directory.CreateDirectory(pcSubFolder);
            }
            foreach (MediaFileInfo file in folder.EnumerateFiles())
            {
                ProcessLightroomFile(file, pcSubFolder);
            }
            foreach (MediaDirectoryInfo subFolder in folder.EnumerateDirectories())
            {
                ProcessLightroomFolder(subFolder, pcSubFolder, true);
            }
        }

        static void ProcessLightroomFile(MediaFileInfo file, string pcFolder)
        {
            Console.WriteLine($"Processing Lightroom file {file.Name} into folder {pcFolder}");
            DownloadFile(file, pcFolder);
        }

        static void DownloadFile(MediaFileInfo phoneFile, string destinationFolder)
        {
            try
            {
                MemoryStream memoryStream = new System.IO.MemoryStream();
                thePhone.DownloadFile(phoneFile.FullName, "c:/misc/downloadedFile.dng");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

在github上提出了一个解决此问题的问题(https://github.com/Bassman2/MediaDevices/issues/32)。问题是您正在基于.NET Core 3.1构建应用程序,并且该库在.NET Framework 4. *上运行良好,因为它们在构建项目时会发出警告:

使用'.NETFramework,Version = v4.6.1,.NETFramework,Version = v4.6.2,.NETFramework,Version = v4.7,.NETFramework,Version = v4.7.1,还原了软件包'MediaDevices 1.8.0', .NETFramework,Version = v4.7.2,.NETFramework,Version = v4.8',而不是项目目标框架'.NETCoreApp,Version = v3.1'。该软件包可能与您的项目不完全兼容