简体   繁体   English

使用WPD(Windows便携设备)Apis传输图像文件时出错

[英]Errors while using WPD (Windows Portable Device) Apis for transferring image files

I have been trying to write a sample application in c# using WPD Apis for transferring image files to a connected WPD supported device. 我一直在尝试使用WPD Apis在c#中编写一个示例应用程序,用于将图像文件传输到连接的WPD支持的设备。 I have been following THIS link. 我一直关注这个链接。 My problem is that everytime i try and transfer a file i keep getting the error: Value does not fall within expected range. 我的问题是每次我尝试传输文件时我都会收到错误:值不在预期范围内。 Has anyone tried doing the same successfully. 有没有人试过成功做同样的事情。 Any pointers are highly appreciated. 任何指针都非常感谢。

Below is the code snippet where i face the error 下面是我面临错误的代码片段

IPortableDeviceContent content;
        this._device.Content(out content);

        IPortableDeviceValues values = 
            GetRequiredPropertiesForContentType(fileName, parentObjectId);

        PortableDeviceApiLib.IStream tempStream;
        uint optimalTransferSizeBytes = 0;
        content.CreateObjectWithPropertiesAndData(
            values,
            out tempStream,
            ref optimalTransferSizeBytes,
            null);           

        System.Runtime.InteropServices.ComTypes.IStream targetStream = 
            (System.Runtime.InteropServices.ComTypes.IStream) tempStream;
        try
        {
            using (var sourceStream = 
                new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                var buffer = new byte[optimalTransferSizeBytes];
                int bytesRead;
                do
                {
                    bytesRead = sourceStream.Read(
                        buffer, 0, (int)optimalTransferSizeBytes);
                    IntPtr pcbWritten = IntPtr.Zero;
                    targetStream.Write(
                        buffer, (int)optimalTransferSizeBytes, pcbWritten);
                } while (bytesRead > 0);
            }
            targetStream.Commit(0);
        }
        finally
        {
            Marshal.ReleaseComObject(tempStream);
        }

The error appears in the line targetStream.Write(... And below is how i have set the parameters. I think there is something wrong with the parameters that i am setting or i am missing some required params. 错误出现在targetStream.Write行中(...以下是我如何设置参数。我认为我设置的参数有问题或者我错过了一些必需的参数。

IPortableDeviceValues values = 
            new PortableDeviceTypesLib.PortableDeviceValues() as IPortableDeviceValues;

        var WPD_OBJECT_PARENT_ID = new _tagpropertykey();
        WPD_OBJECT_PARENT_ID.fmtid = 
            new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 
                     0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
        WPD_OBJECT_PARENT_ID.pid = 3 ;
        values.SetStringValue(ref WPD_OBJECT_PARENT_ID, parentObjectId);

        FileInfo fileInfo = new FileInfo(fileName);
        var WPD_OBJECT_SIZE = new _tagpropertykey();
        WPD_OBJECT_SIZE.fmtid = 
            new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 
                     0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
        WPD_OBJECT_SIZE.pid = 11;            
        values.SetUnsignedLargeIntegerValue(WPD_OBJECT_SIZE, (ulong) fileInfo.Length);

        var WPD_OBJECT_ORIGINAL_FILE_NAME = new _tagpropertykey();
        WPD_OBJECT_ORIGINAL_FILE_NAME.fmtid = 
            new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 
                     0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
        WPD_OBJECT_ORIGINAL_FILE_NAME.pid = 12;
        values.SetStringValue(WPD_OBJECT_ORIGINAL_FILE_NAME, Path.GetFileName(fileName));

        var WPD_OBJECT_NAME = new _tagpropertykey();
        WPD_OBJECT_NAME.fmtid = 
            new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 
                     0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
        WPD_OBJECT_NAME.pid = 4;
        values.SetStringValue(WPD_OBJECT_NAME, Path.GetFileName(fileName));

        var WPD_OBJECT_FORMAT = new _tagpropertykey();
        WPD_OBJECT_FORMAT.fmtid = new Guid(0xef2107d5, 0xa52a, 0x4243, 0xa2, 0x6b, 0x62, 0xd4, 0x17, 0x6d, 0x76, 0x03);
        WPD_OBJECT_FORMAT.pid = 6;
        values.SetGuidValue(WPD_OBJECT_FORMAT, WPD_OBJECT_FORMAT.fmtid);

It will be helpful if you can give further details, such as which line(s) of code is giving this error, the manufacturer/model of the WPD device(s) you were trying with, etc. 如果您可以提供更多详细信息,例如代码的哪一行给出此错误,您尝试使用的WPD设备的制造商/型号等,将会很有帮助。

This error is fairly generic and could mean that the parameters are not formatted correctly, or that you're giving out of range parameters when calling CreateObjectWithPropertiesAndData. 此错误相当通用,可能意味着参数格式不正确,或者您在调用CreateObjectWithPropertiesAndData时给出了范围参数。 If this is the case, then it will help to show the values of the parameters you are passing into the device. 如果是这种情况,那么它将有助于显示您传递到设备中的参数的值。

I have been working on this same error for quite a while now and I think I might have found it. 我一直在研究同样的错误已经有一段时间了,我想我可能已经找到了它。 In my case, the problem was in the code setting WPD_PARENT_OBJECT_ID 就我而言,问题出在代码设置WPD_PARENT_OBJECT_ID中

In most examples, I saw that everyone was setting the object id like this: 在大多数示例中,我看到每个人都像这样设置对象ID:

string parentObjectId = "InternalStorage/SomeFolder/Parent";

var WPD_OBJECT_PARENT_ID = new _tagpropertykey();
WPD_OBJECT_PARENT_ID.fmtid = 
    new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 
             0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
WPD_OBJECT_PARENT_ID.pid = 3 ;
values.SetStringValue(ref WPD_OBJECT_PARENT_ID, parentObjectId);

But in actuality, they literally want the id of the folder, which is completely different from the folder path. 但实际上,他们确实需要文件夹的id,这与文件夹路径完全不同。

//The object id depends on what the device assigns to the folder
string parentObjectId = "o6AC"; //Just as an example from my device

var WPD_OBJECT_PARENT_ID = new _tagpropertykey();
WPD_OBJECT_PARENT_ID.fmtid = 
    new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 
             0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
WPD_OBJECT_PARENT_ID.pid = 3 ;

When I set up my values in this way, the target stream had no problem writing to the device. 当我以这种方式设置我的值时,目标流没有写入设备的问题。

Just for some extra background info, the object id comes from the IPortableDeviceContent variable 只是为了获得一些额外的背景信息,对象id来自IPortableDeviceContent变量

IPortableDeviceContent content;
Device.Content( out content ); //Device should be set elseware

IEnumPortableDeviceObjectIDs objectIds;
content.EnumObjects( 0, parent.Id, null, out objectIds );

//objectIds is a collection of all the objects
uint fetched; //lets us know if an object was grabbed or not

//This is the actual Object Id of that specific folder on the device
//Populated by the ObjectIds.Next function
string ActualObjectId;

objectIds.Next( 1, out ActualObjectId, ref fetched );

Anyways I hope this helps! 无论如何,我希望这有帮助!

The Valence Palacio's answer works! Valence Palacio的答案有效!

You can download the source code of the example, to transfer a file to a portable device using WPDApi, from here ( https://dl.dropboxusercontent.com/u/40603470/WPDTransferToDevice.zip ). 您可以从此处下载该示例的源代码,以使用WPDApi将文件传输到便携式设备( https://dl.dropboxusercontent.com/u/40603470/WPDTransferToDevice.zip )。 Just remember to use the ID of the folder (eg o6AC) instead of the full path. 只需记住使用文件夹的ID(例如o6AC)而不是完整路径。

var devices = new PortableDeviceCollection();
devices.Refresh();
var kindle = devices.First();
kindle.Connect();

kindle.TransferContentToDevice(@"d:\temp\Kindle_Users_Guide.azw", "06AC");

kindle.Disconnect();

I searched for a long time to come up with a complete solution, which this is. 我搜索了很长时间才找到一个完整的解决方案,这就是。

I first want to give Christophe Geer a really big hand. 我首先想给Christophe Geer一个非常大的手。 He is the only person that I have found that had any example of transferring data to a phone using WPD (Windows Portable device) in c#. 他是我发现的唯一一个在c#中使用WPD(Windows便携设备)将数据传输到手机的人。

There were only a few bugs that I had to fix and only a few important features that were missing that I added, such as file size and copying folders. 我只需要修复一些错误,并且只添加了一些我添加的重要功能,例如文件大小和复制文件夹。

The result are a set of PortableDevice files that can be used to do most of the needed CRUD operations. 结果是一组PortableDevice文件,可用于执行大多数所需的CRUD操作。

Check out my github site for the code and more details: 查看我的github网站以获取代码和更多详细信息:

https://github.com/pstorli/WPDFileTransfer https://github.com/pstorli/WPDFileTransfer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM