I want to copy at least one file to a Windows phone through MTP. I can connect to the phone and copy files from the phone to the computer, following this guide:
WPD: transferring content
However, I cannot copy files in the reverse order (from computer to phone). This is my code:
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);
}
I tested this code on multiple devices. It works on a regular mp3 player and assumes the correctness of the tutorial, it also works on an Android phone. But by running this code with two different Windows phones, I get the following exception:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in PortableDevices.exe
Additional information: The data area passed to a system call is too small. (Exception from HRESULT: 0x8007007A)
in this line: targetStream.Write(buffer, (int)optimalTransferSizeBytes, pcbWritten);
The buffer size is 262144 bytes, while the file size is only 75 KB. Hope someone has an idea how to solve this problem.
Hello j0h4nn3s