我需要从我的Android应用程序读取和写入USB记忆棒上的数据文件 . 到目前为止,我没有取得多大成功 .

我这样做的主要参考是Android Developer的网站文档:http://developer.android.com/guide/topics/connectivity/usb/host.html

我在这个主题上找到的最接近的Stack Overflow答案是10183794 .

我正在使用通用的无名Android平板电脑开发和测试我的代码 . 它运行的是Android 4.2.2 .

平板电脑有一个主机USB端口,我可以直接插入我的USB记忆棒 . 平板电脑没有OTG micro USB连接器 .

当我将USB记忆棒插入平板电脑时,平板电脑本身会检测到它 . 我可以看到USB记忆棒安装在:/ mnt / usbhost1 .

因此,虽然平板电脑可以检测到USB记忆棒的存在,但我正在开发的应用程序似乎没有看到它 .

Android开发者USB主机文档介绍了两种检测USB记忆棒的方法:1 . 发现设备和2.枚举设备 . 这两种方法都不适合我 .

我的Manifest.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android1"
   android:versionCode="1"
   android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="17" />

<uses-permission
    android:required="true"
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>      

<uses-permission
    android:required="true"
    android:name="android.permission.READ_EXTERNAL_STORAGE"/>   

<uses-feature android:name="android.hardware.usb.host" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.android1.MainActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name" >
      <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
      <intent-filter>
         <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />  
         <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>  
      <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" 
                  android:resource="@xml/device_filter" />       
    </activity>
    <receiver android:name=".UsbBroadcastReceiver" >
    </receiver>             
</application>

上面引用的xml / device_filter文件如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <usb-device />
</resources>

我没有为usb-device提供vendor-id,product-id或任何其他属性,因为文档声明未指定它们将匹配每个USB设备,这就是我想要的 .

当我运行应用程序并尝试在USB上打开数据文件时,应用程序不会显示一个对话框,要求我授予应用程序访问USB记忆棒的权限 . 根据我在USB Host文档中看到的内容,我应该收到一条对话框消息 .

相反,我收到以下错误消息:

07-17 13:34:14.630: D/tom5(4639): error msg: /mnt/usbhost1/smartshoe-data/SS-History-20150717-1334-14.txt: open failed: EACCES (Permission denied

然后我尝试了“枚举设备”的方法 . 我添加到我的应用程序中的代码如下:

public class MainActivity extends Activity {
   private final String ACTION_USB_PERMISSION= "com.example.android1.USB_PERMISSION";

   private final IntentFilter intentFilterUsb = new IntentFilter();
   UsbManager mUsbManager;
   UsbBroadcastReceiver mReceiverUsb;
   PendingIntent UsbPermissionIntent; 
   UsbDevice device;
   ..................

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    WiflyMain.mainActivity = this; 

    setContentView(R.layout.form4splashscreenss1); 
    intentFilterUsb.addAction(ACTION_USB_PERMISSION);     
    .............

    mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);   
    mReceiverUsb = new UsbBroadcastReceiver(mUsbManager);     
    registerReceiver(mReceiverUsb, intentFilterUsb);    
    UsbPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION),0);

    //Check to see if the app sees a USB device
    HashMap<String,UsbDevice> deviceList = mUsbManager.getDeviceList();
    Log.d("tom5","DEVICE LIST = " + deviceList);
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
    while(deviceIterator.hasNext())
      {
        UsbDevice deviceXX = deviceIterator.next();
        Log.d("tom5","DEVICE = " + deviceXX.toString());
        Log.d("tom5","DEVICE NAME =" + deviceXX.getDeviceName());
      }//while 
    //mUsbManager.requestPermission(device, UsbPermissionIntent);

当我在平板电脑中插入USB运行此代码时,我收到以下日志消息:

07-17 13:26:38.770: D/tom5(3613): DEVICE LIST = {}

表示USB设备列表为空 . 显然我的应用程序没有看到USB记忆棒 .

我注释掉了对mUsbManager.requestPermission的调用,因为如果device参数为null,它将使应用程序崩溃 .

我没有在这里包含UsbBroadcastReceiver代码,因为应用程序从不执行它(构造函数代码除外) .

有什么我在这里失踪,让我的应用程序识别USB棒?您可以提供的任何帮助将不胜感激 . 谢谢 .