如何通过Android SDK访问USB Stick的文件系统?

时间:2015-05-24 12:17:06

标签: android usb

Here另一位开发人员试图访问Android SDK中的USB Stick文件系统。

但USB主机模式只能以原始模式访问USB设备。 Here Android SDK文档告诉我们FileStream-Operations在USB附件模式下可用

但是如何实现呢?代码示例都是不完整的。

以下是代码:

private TextView textView;
private PendingIntent mPermissionIntent;
private static final String ACTION_USB_PERMISSION =
        "com.android.example.USB_PERMISSION";
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) {
        textView = (TextView) findViewById(R.id.textView);
        textView.setText("onReceive()... ");
        String action = intent.getAction();
        if (ACTION_USB_PERMISSION.equals(action)) {
            synchronized (this) {
                UsbAccessory accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);

                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                    textView.setText("accessing... ");
                    if(accessory != null){
                        //call method to set up accessory communication
                    }
                }
                else {
                    Log.d("onCreate()", "permission denied for accessory " + accessory);
                    textView.setText("permission denied for accessory ");
                }
            }
        }
    }
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_slideshow);


    UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    registerReceiver(mUsbReceiver, filter);
}

但我的应用程序无法识别任何已连接的USB记忆棒。该程序甚至没有跳转到onReceive() - 方法。

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="slideshow.xappo.de.xappo_slideshow" >

<uses-feature android:name="android.hardware.usb.accessory" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity

        android:name=".Slideshow"
        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_ACCESSORY_ATTACHED" />
        </intent-filter>
    </activity>
</application>

有人有示例代码如何在USB记忆棒上创建目录并从USB记忆棒复制一些文件吗?

最好的问候

1 个答案:

答案 0 :(得分:0)

I have found a "dirty" solution. I just access "/storage/usbdisk" with standard Java File-I/O. This works on two Devices. On a Motorola Moto G and on a Sony xPeria Z1. But I don't know if every Android 4.x Device mounts USB sticks to "/storage/usbdisk". Maybe someone knows? My App has to run on an Android 4.x Tablet.

And still I'd like to make my app recognize that an USB device was plugged/ unplugged. But the Broadcast Receiver in my posted code doesn't work. How to fix that?

相关问题