preface

There is one requirement: The Android OTG connects to a USB device (a wearable camera with an SD card) and reads files from its memory to upload to the network

The problem

The biggest difficulty of this requirement lies in reading the files in the USB device, and the API interface we upload requires uploading files with File as the parameter. Android, on the other hand, provides a very low-level API for bulk Transfer.

Knowledge points

OTG

On the Go is mainly used to connect and exchange data between USB and different mobile devices, which is used to connect mobile devices like mobile phones and USB devices.

File System Type

Common file system types include FAT12, FAT16, FAT32, NTFS, and HFS (Apple’s). Fat16 only supports less than 2GB of storage, and FAT32 supports up to 32GB, but a single file cannot exceed 4G. NTFS can support more than 4G of a single file

MBR

Master boot record, a summary of information, is equivalent to a disk contains how many partitions, partition the file system type is what, have greatly, logical address range, and so on, how much is 512 bytes, each 16 after the 446 as a partitioned table, each partition table entry 5 is to show that the file system type for the partition

GPT

A more advanced partition table than MBR, mainly because MBR supports up to 2TB disks with 4 partitions, while GPT supports more and is compatible with MBR. Most computer hard drives today are GPT

We needed a file system to support it, so we searched GitHub and found a Libaums framework.

Libaums
  • It’s a great library, and the author is great and patient enough to test the survey questions with me
  • Support OTG to connect mobile phones to read USB Mass storage devices, and add support for FAT32 file system type devices on the basis of Android USB Host API, so that we can access its file directory by accessing the file path similar to the file tree.
  • A file is represented as UsbFile, but there is no way to get the actual mount path directly from UsbFile (because Android wants us to use FileProvider for file sharing between applications for file security purposes). And, although Android is based on the Linux kernel, Android does not support directly reading files from mounted USB.)
  • Libaums is also written based on the Android Api, so it meets the requirements of the Android system, so the integration needs to be configured in the Manifest
  • UsbFile does not get the File path directly so that we can operate on File. Instead, we provide a UsbFileInputStream class that reads the output stream of UsbFile. We can get File objects through the stream.

The development of

Since Libaums and Android systems do not support it, we have to read the files on the Usb device in the form of streams, and write them to the phone and save them as files (copy to the phone), and then upload them. Finally, delete the copy file from the phone.

The question

Everything worked fine on a Usb drive, but when tested on a Usb device, it broke down. GetPartition is empty, so the device file cannot be read. Why is partition empty?

To solve

I communicated with the author on the issue for a long time, and I also tested it constantly. I finally figured it out. Usb device is a camera of Kehan Company, with SD card, photos taken will be stored on the SD card. Partition stands for partition. Libaums can be read by devices with MBR partitions and file system type FAT32. The diskutil list command allows you to view the path of usb devices hanging on your computer

bin-no-MBP:Volumes bingao$ diskutil list
/dev/disk0 (internal, physical):
   #: TYPE NAME SIZE IDENTIFIER
   0:      GUID_partition_scheme                        *251.0 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:          Apple_CoreStorage Macintosh HD            250.1 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3

/dev/disk1 (internal, virtual):
   #: TYPE NAME SIZE IDENTIFIERZero: Apple_HFS Macintosh HD +249.8 GB Disk1 Logical Volume on Disk0S2 8ABB2546-043F-4E17-A20A-8eECAB36be00 Unlocked  /dev/disk2 (external, physical):#: TYPE NAME SIZE IDENTIFIER0: NO NAME x 31.9 GB disk2Copy the code

Linux: hexdump -c -s 446-n 64 /dev/disk2: hexdump -c -s 446-n 64 /dev/disk2

bin-no-MBP:Volumes bingao$ sudo hexdump -C -s446 -n 64 /dev/disk2 000001be 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................ | * 000001feCopy the code

Original: There are many SD cards or storage devices to save memory, so even the 512 MBR sector was removed.

Finally in the disk utility after erasing the reformat to solve.

See Libaums Uri to File Path or usbFile to file Libaums Investigate on SD card issues

PS

In addition, many third-party DIsk tools on MAC are charged. After searching for a long time on the Internet, I found a very good tool, Hard DIsk Manager, mainly because it can be tried free for 10 days and has many functions

[image-a4c2B2-1542956165144] [image-a4c2B2-1542956165144] [upload_image.jianshu. IO /upload_imag…]

The purpose of this article is to sort out and record the problems I have encountered, and what I have learned during the investigation and solution of the problems, as well as to provide some ideas for some friends who have similar needs.