我需要获得连接USB闪存到我手机的路径,然后找到文件。 但是,我有一些问题。
我的宣言:
< uses-feature android:name =“android.hardware.usb.host”/>
< uses-permission android:name =“android.permission.USB_PERMISSION”/>
我的BroadcastReceiver
private final String TAG = "USBDevicesReceiver";
UsbDevice usbDevice;
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, intent.getAction());
if (intent.getAction().equalsIgnoreCase("android.hardware.usb.action.USB_STATE")){
if(intent.getExtras().getBoolean("connected")) {
Log.d(TAG, "connected");
usbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
try {
String path = usbDevice.getDeviceName();
Log.d(TAG, path);
Toast.makeText(context, path, Toast.LENGTH_SHORT).show();
if (FindFile.isFileFound(path, "log.txt")) {
Toast.makeText(context, "YES", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "NO", Toast.LENGTH_SHORT).show();
}
} catch (Exception ex){
Log.e(TAG, ex.toString());
}
}
}
}
但是usbDevice在这一刻返回null:usbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);