这个想法很简单:
使用音乐(mp3)文件附加USB pendrive,让android 4.0访问它。 例如,播放音乐。
首先:
该应用拥有访问设备的正确权限
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
当我附上pendrive时,应用程序会请求许可,我会给予它。
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)){
Log.d("USB","permission OKE");
if(device instanceof UsbDevice){
test(mUsbManager,device);
}
}
void test(UsbManager mUsbManager, UsbDevice device){
UsbInterface intf = device.getInterface(0);
UsbDeviceConnection connection = mUsbManager.openDevice(device);
if(connection.claimInterface(intf, true)){
Log.d("open","YES OPENS WORKS");
// TRYING THINGS AT THIS PLACE
try {
// String line;
// Process p = Runtime.getRuntime().exec("ls /mnt/USB0");
// BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()) );
// while((line = in.readLine()) != null){
// Log.d("read",line);
// }
// in.close();
}
catch (Exception e) {
Log.d("command error:",e.getMessage());
}
connection.releaseInterface(intf);
}else{
Log.d("open","cannot claim interface");
}
connection.close();
}
不符合上述事实的解决方案