androidmediastoreandroid-external-storageusb-drive

How to access Media files (such as images/video) present on a USB stick connected to a device?


I am building a File explorer kind of app for Android, which shows a categorized view of all files present in internal and external storage(s). For example to list all Video files present on device's storage, I applied the following code:

private void loadVideos()
    {
        Cursor cursor;
        Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Video.Media.BUCKET_DISPLAY_NAME,MediaStore.Video.Media._ID,MediaStore.Video.Thumbnails.DATA,MediaStore.Video.Media.TITLE, MediaStore.Video.Media.DURATION};
        final String orderBy = MediaStore.Video.Media.TITLE;
        int data,folder,id,thumb,title,duration;

        cursor = getApplicationContext().getContentResolver().query(uri, projection,null,null, orderBy);

        id      = cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID);
        thumb   = cursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA);
        title   = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE);
        duration= cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);

        while(cursor.moveToNext()) {
            long length = Long.parseLong(cursor.getString(duration)) / 1000;
            long minutes = length / 60;
            long seconds = length % 60;

            items.add(new VideoItem(cursor.getString(title),cursor.getString(thumb),cursor.getString(id),properFormat(minutes)+":"+properFormat(seconds)));

        }
        adapter.notifyDataSetChanged();
        cursor.close();
    }

Everything works fine, except for when I attach a USB device. In which case it shows only those videos present on device, and doesn't show video files from the USB device.

Things that I have already checked:

1) Permissions for external storage is granted.
2) USB device is successfully mounted because it shows up in Settings as well as other apps such as MX Player shows video files from USB device.

I also have doubt regarding the Uri which currently is MediaStore.Video.Media.EXTERNAL_CONTENT_URI.

But according to the name it feels like, it should have done the trick, although its clearly not working for external devices? Can anyone please guide me where I might be going wrong? The goal is to show media files from Phone's internal storage, external storage (like Micro SD card) and from any external USB device (if attached). Thanks.


Solution

  • UsbConstants states, thatUSB_CLASS_MASS_STORAGE would be 8 - therefore I'd say, that you need to reference device_filter.xml in the Manifest.xml, where the registering Activity is the one which shall handle the media.

    <activity
    ...
    >
        ...
        <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>
    

    and then filter in that referenced device_filter.xml for the device class 8:

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