简体   繁体   中英

Android transfer sqlite database to USB flash drive from sdcard programatically

I have a custom built Android device which is Single Board Computer with a display unit. It has Android 4.1 installed on it and has a USB port. Within an app I created an sqlite database. I want to transfer the database to a usb flash drive using the aforementioned USB port. I understand Android documentation enough to be able to establish a connection between USB host and Accessory. I am able to detect my flash drive using an intent filter.

The following is a code snippet to transfer a byte array using USB classes.

     private Byte[] bytes
     private static int TIMEOUT = 0;
     private boolean forceClaim = true;

     ...

     UsbInterface intf = device.getInterface(0);
     UsbEndpoint endpoint = intf.getEndpoint(0);
     UsbDeviceConnection connection = mUsbManager.openDevice(device); 
     connection.claimInterface(intf, forceClaim);
     connection.bulkTransfer(endpoint, bytes, bytes.length, TIMEOUT); 

Here a bytearray is transferred using the USB interface. I want to be able to transfer an sqlite database using the same. Is that possible? How can I do it?

As is documented in several other questions here, Android APIs support only raw transfers, so you would have to implement an entire filesystem in your app.

Since your device is custom, you would be better off creating a Linux-level daemon or (bulletproof!) setuid tool to mount the USB drive at operating system level, and leverage the filesystem code already present in the Lunux kernel. Then you can simply perform normal file operations to it. You might even be able to modify Android's vold to do this - because you control the Android install, you have this class of options which a typical 3rd party developer targeting locked down phones does not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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