访问连接的 MTP 设备而不接收 USB_DEVICE_ATTACHED Intent?

发布于 2024-12-08 06:42:19 字数 1263 浏览 4 评论 0原文

我正在开发一些应用程序,它使用媒体传输协议从连接到我的 Android 平板电脑 USB 主机(Android 3.1、Acer Iconia Tab A500)的相机读取照片。

我找到了一些描述如何使用 USB 的教程,并且我已经可以从 USB 摄像头复制到 SD 卡了。

我只有一个问题:所有教程都等待 USB_DEVICE_ATTACHED-Intent,然后打开 UsbDeviceConnection 以便与意图捆绑包中发送的设备进行通信。

所以我希望能够手动启动一个访问 USB 的活动 - 而不接收此意图。 现在的问题是,我无法像这样打开 USBConnection

UsbDeviceConnection usbDeviceConnection = usbManager.openDevice(usbDevice);

它只是返回 null。 如果我重新插入设备并选择我的活动作为意图接收器,它就可以正常工作。 我想这一定是因为IntentReceiver对USB设备有一些独占的权限。

我的清单如下所示:

    <activity
        android:name=".ImageActivity"
        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" />
        </intent-filter>
        <meta-data
            android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
            android:resource="@xml/device_filter" />
    </activity>

举个例子:如果用户将 Galery 设置为 USB 设备的默认设置,该怎么办?这种情况下如何访问USB设备呢?

I´m working on some application which uses Media Transfer Protocol to read Photos from a Camera connected to my Android Tablets USB host (Android 3.1, Acer Iconia Tab A500).

I found some tutorials describing how to work with USB and I got copying from USB camera to SD-Card already working.

There is only one problem I have: All the tutorials wait for USB_DEVICE_ATTACHED-Intent and then open a UsbDeviceConnection for communicating with the device that is sent within the intents Bundle.

So I would like to be able to manually start an activity which accesses USB - WITHOUT receiving this intent.
The Problem now is, that I cannot open an USBConnection like this

UsbDeviceConnection usbDeviceConnection = usbManager.openDevice(usbDevice);

It simply returns null.
If I re-plugin the device and select my Activity as Intent receiver, it works fine.
I think this must be because of the IntentReceiver having some exclusive permissions on the USB device.

My Manifest looks like this:

    <activity
        android:name=".ImageActivity"
        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" />
        </intent-filter>
        <meta-data
            android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
            android:resource="@xml/device_filter" />
    </activity>

So one example: What if a user has set Galery as default for USB devices. How can I access the USB device in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若沐 2024-12-15 06:42:19

我发现 USB 主机模式 API 指南文档在该领域非常有用。

http://developer.android.com/guide/topics /connectivity/usb/host.html#discovering-d

最重要的注意事项是,如果您没有从活动中收到 UsbDevice,那么您将需要请求与其通信的权限。提供的链接中描述了此过程。

I have found the USB Host mode API guide documentation to be highly useful in this area.

http://developer.android.com/guide/topics/connectivity/usb/host.html#discovering-d

The most important note is that if you have NOT received a UsbDevice from an activity, then you will need to request permission to communicate with it. This process is described in the link provided.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文