ArrayList, UsbMassStorageDevice的磁區資訊
[[String VolumeLabel, int PartitionType, Long Capacity, Long OccupiedSpace, Long FreeSpace, int ChunkSize]]
2 : FAT32
6 : ExFAT
[["PartitionNo1", 2, 10000, 5000, 5000, 32768],["PartitionEx", 6, 10000, 6000, 4000, 532]]
UsbMassStorageDevice[] devices = UsbMassStorageDevice.getMassStorageDevices(this /* Context or Activity */);
//通常Android只有一個 Usb 傳輸插槽
final int currentDevice = 0;
//before interacting with a device you need to call init()!
devices[currentDevice].init()
//印出UsbMassStorageDevice的磁區資訊
ArrayList partitions = new TGMassStorageDevice(devices[currentDevice]).getPartitions();
Log.e(partitions.toString())
//磁區index
Int currentPartition = 0;
FileSystem currentFs = devices[currentDevice].getPartitions().get(0).getFileSystem();
IOException
UsbFile, 絕對路徑的UseFile檔案
TGFileOperationException : 當filePathName檔案不存在時拋出
TGFileOperation fileOperation = new TGFileOperation(currentFs, currentFs.getRootDirectory());
UsbFile file = fileOperation.getFile("/dir1/subdir2/picture.jpg");
ArrayList, 目錄下所有檔案, 暫不包含子資料夾
TGFileOperationException : 當找不到目錄時拋出
TGFileOperation fileOperation = new TGFileOperation(currentFs, currentFs.getRootDirectory());
ArrayList arrayList = fileOperation.getFileListIn("/dir/subdir");
將單/多筆UsbFile寫入指定路徑, 單一檔案寫入完畢後會開啟
TGFileOperationException :
當找不到路徑時拋出
TGFileOperation fileOperation = new TGFileOperation(currentFs, currentFs.getRootDirectory());
ArrayList arrayList = fileOperation.getFileListIn("/123");
fileOperation.fileCopyToPhone(MainActivity.this, arrayList,
Environment.getExternalStorageDirectory().getAbsolutePath());
A library to access USB mass storage devices (pen drives, external HDDs, card readers) using the Android USB Host API. Currently it supports the SCSI command set and the FAT32 file system.
I am currently trying to understand who and how people use this library as well as where is most room for future improvement. To do so I created a really short survey (no more than 3 minutes) and I would be really happy if people would take part: https://forms.gle/WBkXWpPVRYxrPhrs9
Thank you!
The library can be included into your project like this:
compile 'com.github.mjdev:libaums:0.7.1'
If you need the HTTP or the storage provider module:
compile 'com.github.mjdev:libaums-httpserver:0.5.3'
compile 'com.github.mjdev:libaums-storageprovider:0.5.1'
UsbMassStorageDevice[] devices = UsbMassStorageDevice.getMassStorageDevices(this /* Context or Activity */);
for(UsbMassStorageDevice device: devices) {
// before interacting with a device you need to call init()!
device.init();
// Only uses the first partition on the device
FileSystem currentFs = device.getPartitions().get(0).getFileSystem();
Log.d(TAG, "Capacity: " + currentFs.getCapacity());
Log.d(TAG, "Occupied Space: " + currentFs.getOccupiedSpace());
Log.d(TAG, "Free Space: " + currentFs.getFreeSpace());
Log.d(TAG, "Chunk size: " + currentFs.getChunkSize());
}
val devices = UsbMassStorageDevice.getMassStorageDevices(this /* Context or Activity */)
for (device in devices) {
// before interacting with a device you need to call init()!
device.init()
// Only uses the first partition on the device
val currentFs = device.partitions[0].fileSystem
Log.d(TAG, "Capacity: " + currentFs.capacity)
Log.d(TAG, "Occupied Space: " + currentFs.occupiedSpace)
Log.d(TAG, "Free Space: " + currentFs.freeSpace)
Log.d(TAG, "Chunk size: " + currentFs.chunkSize)
}
Your app needs to get permission from the user at run time to be able to communicate the device. From a UsbMassStorageDevice
you can get the underlying android.usb.UsbDevice
to do so.
PendingIntent permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
usbManager.requestPermission(device.getUsbDevice(), permissionIntent);
val permissionIntent = PendingIntent.getBroadcast(this, 0, Intent(ACTION_USB_PERMISSION), 0);
usbManager.requestPermission(device.usDevice, permissionIntent);
For more information regarding permissions please check out the Android documentation: https://developer.android.com/guide/topics/connectivity/usb/host.html#permission-d
UsbFile root = currentFs.getRootDirectory();
UsbFile[] files = root.listFiles();
for(UsbFile file: files) {
Log.d(TAG, file.getName());
if(file.isDirectory()) {
Log.d(TAG, file.getLength());
}
}
UsbFile newDir = root.createDirectory("foo");
UsbFile file = newDir.createFile("bar.txt");
// write to a file
OutputStream os = new UsbFileOutputStream(file);
os.write("hello".getBytes());
os.close();
// read from a file
InputStream is = new UsbFileInputStream(file);
byte[] buffer = new byte[currentFs.getChunkSize()];
is.read(buffer);
val root = currentFs.rootDirectory
val files = root.listFiles()
for (file in files) {
Log.d(TAG, file.name)
if (file.isDirectory) {
Log.d(TAG, file.length)
}
}
val newDir = root.createDirectory("foo")
val file = newDir.createFile("bar.txt")
// write to a file
val os = UsbFileOutputStream(file)
os.write("hello".toByteArray())
os.close()
// read from a file
val ins = UsbFileInputStream(file)
val buffer = ByteArray(currentFs.chunkSize)
ins.read(buffer)
OutputStream os = UsbFileStreamFactory.createBufferedOutputStream(file, currentFs);
InputStream is = UsbFileStreamFactory.createBufferedInputStream(file, currentFs);
// Don't forget to call UsbMassStorageDevice.close() when you are finished
device.close();
Usually third party apps do not have access to the files on a mass storage device if the Android system does mount (this is usually supported on newer devices, back in 2014 there was no support for that) the device or this app integrates this library itself. To solve this issue there are two additional modules to provide access to other app. One uses the Storage Access Framework feature of Android (API level >= 19) and the other one spins up an HTTP server to allow downloading or streaming of videos or images for instance.
libaums currently supports two different HTTP server libraries.
You can spin up a server pretty easy, you just have to decide for a HTTP server implementation. If you do not have special requirements, you can just go for one, it should not make much of a difference.
UsbFile file = ... // can be directory or file
HttpServer server = AsyncHttpServer(8000); // port 8000
// or
HttpServer server = NanoHttpdServer(8000); // port 8000
UsbFileHttpServer fileServer = new UsbFileHttpServer(file, server);
fileServer.start();
val file: UsbFile
// can be directory or file
val server = AsyncHttpServer(8000) // port 8000
// or
val server = NanoHttpdServer(8000) // port 8000
val fileServer = UsbFileHttpServer(file, server)
fileServer.start()
The file you provide can either be an actual file or a directory:
If you want to be able to access these files when your app is in background, you should implement a service for that. There is an example available in the httpserver
module. You can use it, but should subclass it or create your own to adapt it to your needs.
private UsbFileHttpServerService serverService;
ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "on service connected " + name);
UsbFileHttpServerService.ServiceBinder binder = (UsbFileHttpServerService.ServiceBinder) service;
serverService = binder.getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "on service disconnected " + name);
serverService = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
...
serviceIntent = new Intent(this, UsbFileHttpServerService.class);
...
}
@Override
protected void onStart() {
super.onStart();
startService(serviceIntent);
bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}
private void startHttpServer(final UsbFile file) {
...
serverService.startServer(file, new AsyncHttpServer(8000));
...
}
private var serverService: UsbFileHttpServerService? = null
internal var serviceConnection: ServiceConnection = object : ServiceConnection() {
override fun onServiceConnected(name: ComponentName, service: IBinder) {
Log.d(TAG, "on service connected $name")
val binder = service as UsbFileHttpServerService.ServiceBinder
serverService = binder.getService()
}
override fun onServiceDisconnected(name: ComponentName) {
Log.d(TAG, "on service disconnected $name")
serverService = null
}
}
override protected fun onCreate(savedInstanceState: Bundle) {
serviceIntent = Intent(this, UsbFileHttpServerService::class.java)
}
override protected fun onStart() {
super.onStart()
startService(serviceIntent)
bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE)
}
See the example app for additional details on that.
To learn more about this visit: https://developer.android.com/guide/topics/providers/document-provider.html
To integrate this module in your app the only thing you have to do is add the definition in your AndroidManifest.xml.
<provider
android:name="com.github.mjdev.libaums.storageprovider.UsbDocumentProvider"
android:authorities="com.github.mjdev.libaums.storageprovider.documents"
android:exported="true"
android:grantUriPermissions="true"
android:permission="android.permission.MANAGE_DOCUMENTS"
android:enabled="@bool/isAtLeastKitKat">
<intent-filter>
<action android:name="android.content.action.DOCUMENTS_PROVIDER" />
</intent-filter>
</provider>
After that apps using the Storage Access Framework will be able to access the files of the USB mass storage device.
app/
directory you can find an example application using the library.UsbFile.setLength(long)
first. Otherwise the ClusterChain has to be increased for every call to write. This is very inefficent.FileSystem.getChunkSize()
bytes as buffer size, because this alignes with the block sizes drives are using. Everything else is also most likeley a decrease in performance.UsbFileStreamFactory
.Libaums - Library to access USB Mass Storage Devices
License: Apache 2.0 (see license.txt for details)
Author: Magnus Jahnen, jahnen at in.tum.de
Advisor: Nils Kannengießer, nils.kannengiesser at tum.de
Supervisor: Prof. Uwe Baumgarten, baumgaru at in.tum.de
Technische Universität München (TUM)
Lehrstuhl/Fachgebiet für Betriebssysteme
www.os.in.tum.de
The library was developed by Mr. Jahnen as part of his bachelor's thesis in 2014. It's a sub-topic of the research topic "Secure Copy Protection for Mobile Apps" by Mr. Kannengießer. The full thesis document can be downloaded here.
We would appreciate an information email, when you plan to use the library in your projects.