Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
745 views
in Technique[技术] by (71.8m points)

sqlite - Flush MTP connection with Android tablet?

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

  • Step 1. Copy my SQLite database from Windows 7 to tablet via 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. Check it using sqlite3.exe. It's corrupt.

Now another test.

  • Step 1. Copy my SQLite database from Windows 7 to tablet via Windows Explorer
  • Step 2. Disconnect then 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. Check it using sqlite3.exe. It's NOT corrupt.

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

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

My actual Windows application uses the WPD (Windows Portable Devices) API, I'm just testing using Windows Explorer to prove it's not a problem in my Windows code. I don't see anything in WPD to do a flush.

The error seems to occur when the SQLite database on Android grows by a page (or more). The page size in SQLite is 512 bytes. Looking at the SQLite database's binary data , I can see what's happening. I make the database one page bigger on the device, copy the database off the device, unplug the usb, plug it in, then copy it off again. Comparing the two files, the file I get after I copy the usb data off is the same except it has a lot more data at the end. It's like MTP doesn't understand the file is bigger until you unplug the usb and plug it in again. If you leave USB plugged in it only copies over the number of bytes that were there the last time it copied the file.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use this function of windows as shown by this DELPHI example.


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

var Info : TSHFileOpStruct;
Aborted : Bool;

begin
Aborted := False;

with Info do
begin
Wnd := hWndOwner;
wFunc := FO_COPY;

// From Microsoft's Help:
// wFunc = Operation to perform. This member can be one of the following values:
// FO_COPY Copies the files specified by pFrom to the location specified by pTo.
// FO_DELETE Deletes the files specified by pFrom (pTo is ignored).
// FO_MOVE Moves the files specified by pFrom to the location specified by pTo.
// FO_RENAME Renames the files specified by pFrom.

pFrom := pChar(SourceFile);
pTo := pChar(TargetFile);
fFlags := 0;
fFlags := FOF_SILENT or FOF_NOCONFIRMATION or FOF_NOERRORUI;
fAnyOperationsAborted := Aborted;
end;
try
SHFileOperation(Info);
finally
if Aborted then; enact upon any user cancellations
end;
end;

I'm copying file from Desktop to Android MTP device PATH

Stefano www.data-ware.it


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...