简体   繁体   中英

How to fetch media files (Images,videos,audio) from Android local storage?

I am making a Music player App , which will Fetch the Songs from local storage & display them on the Recyclerview. I searched online and found about the MediaStore Class , but that also needs an API Android R. Due to this I was wondering what is the most general way to fetch media files from android devices, which can we applied across different API's.

1- Accessing shared media storage got complicated with recent Android APIs

the most general way is documented here : https://developer.android.com/training/data-storage/shared/media

For kotlin :

val projection = arrayOf(media-database-columns-to-retrieve)
val selection = sql-where-clause-with-placeholder-variables
val selectionArgs = values-of-placeholder-variables
val sortOrder = sql-order-by-clause

applicationContext.contentResolver.query(
    MediaStore.media-type.Media.EXTERNAL_CONTENT_URI,
    projection,
    selection,
    selectionArgs,
    sortOrder
)?.use { cursor ->
    while (cursor.moveToNext()) {
        // Use an ID column from the projection to get
        // a URI representing the media item itself.
    }
}

Audio files, which are stored in the Alarms/, Audiobooks/, Music/, Notifications/, Podcasts/, and Ringtones/ directories, as well as audio playlists that are in the Music/ or Movies/ directories. The system adds these files to the MediaStore.Audio table.

There is an example for video files : https://developer.android.com/training/data-storage/shared/media#query-collection

You can use it for audio files with changing related query parameters

You need read external storage first :

This is may helps you :

public static List<File> getAllMediaFilesOnDevice(Context context) {
        List<File> files = new ArrayList<>();
        try {

            final String[] columns = { MediaStore.Images.Media.DATA,
                    MediaStore.Images.Media.DATE_ADDED,
                    MediaStore.Images.Media.BUCKET_ID,
                    MediaStore.Images.Media.BUCKET_DISPLAY_NAME };

            MergeCursor cursor = new MergeCursor(new Cursor[]{context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, null),
                    context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, columns, null, null, null),
                    context.getContentResolver().query(MediaStore.Images.Media.INTERNAL_CONTENT_URI, columns, null, null, null),
                    context.getContentResolver().query(MediaStore.Video.Media.INTERNAL_CONTENT_URI, columns, null, null, null)
            });
            cursor.moveToFirst();
            files.clear();
            while (!cursor.isAfterLast()){
                String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                int lastPoint = path.lastIndexOf(".");
                path = path.substring(0, lastPoint) + path.substring(lastPoint).toLowerCase();
                files.add(new File(path));
                cursor.moveToNext();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return files;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM