Connect an MTP connection to an Android tablet?

I connect a Samsung Galaxy Android tablet with a USB cable to a computer running Windows 7. It connects via MTP.

  • Step 1. Copy my SQLite database from Windows 7 to your tablet through Windows Explorer.
  • Step 2. Open it on the tablet (which adds the android_metadata table), and then close it.
  • Step 3. Copy the SQLite database back to Windows.
  • Step 4. Verify it using sqlite3.exe. It is damaged.

Now another test.

  • Step 1. Copy my SQLite database from Windows 7 to your tablet using Windows Explorer
  • Step 2. Unplug and reconnect the USB cable.
  • Step 3. Open it on the tablet (which adds the android_metadata table), and then close it.
  • Step 3. Copy the SQLite database back to Windows.
  • Step 4. Verify it using sqlite3.exe. It is NOT damaged.

Interestingly, if I switch steps 2 and 3, it also works.

Since it works, when I disconnect and reconnect the USB cable, I assume that I need to somehow reset the MTP cache. How can this be done or is there an API that I can use to quickly disconnect and reconnect the device?

My real Windows application uses the WPD (Windows Portable Devices) API, I just test with Windows Explorer to prove that this is not a problem in my Windows code. I do not see anything in WPD to make a flash.

The error occurs when the SQLite database on Android grows on a page (or more). SQLite's page size is 512 bytes. Looking at the binary data of the SQLite database, I can see what happens. I make the database one page larger on the device, copy the database from the device, disconnect the USB drive, connect it, and then copy it again. Comparing the two files, the file that I get after I copied the usb data remains the same, except that in the end it has a lot more data. He, like MTP, does not understand that the file is larger until you unplug the USB drive and plug it in again. If you leave the USB connected to it, only the number of bytes that was the last time it was copied is copied.

+4
source share
2 answers

Use this function for windows as shown in this DELPHI example.


shCopyFile procedure (hWndOwner: HWND; const SourceFile, TargetFile: string);

var Info: TSHFileOpStruct;
Interrupted: Bool,

to begin
Aborted: = False,

with info do
to begin
Wnd: = hWndOwner;
wFunc: = FO_COPY;

// From Microsoft Help:
// wFunc = operation to execute. This member can be one of the following values:
// FO _COPY Copy the files specified in pFrom to the location specified by pTo.
// FO _DELETE Deletes the files specified in pFrom (pTo is ignored).
// FO _MOVE Moves the files specified by pFrom to the location specified by pTo.
// FO _RENAME Renames the files specified in pFrom.

pFrom: = pChar (SourceFile);
pTo: = pChar (TargetFile);
fFlags: = 0;
fFlags: = FOF_SILENT or FOF_NOCONFIRMATION or FOF_NOERRORUI;
fAnyOperationsAborted: = Aborted,
end;
to try
SHFileOperation (information);
finally
if it is interrupted; decide on any user cancellations
end;
end;

I copy the file from the desktop to the Android MTP device PATH

Stefano www.data-ware.it

0
source

I found the same problem on Samsung Galaxy Tab 2 7.0.

But in my solution, this is a problem on the Android side.

If I force the application to open db to stop (System Settings-> app manager-> my app-> force stopp), then it works.

Only "open connection" and "close connection" are not enough. The full process on the android side should be closed.

This explains why it works after restarting the device.


Further research: It works fine with the Galaxy Tab and 4.1.2 (if the App process is closed). It works with MTP, but not PTP (Image Transfer Protocol). Both protocols connect to the WPD function, but PTP is completely incompatible when writing and reading (so never use PTP).

0
source

Source: https://habr.com/ru/post/1394419/


All Articles