简体   繁体   中英

Android Show media files from sdcard as well as external storage

I want to show media files (say photos) both from sdcard and phone storage.

Environment.getExternalStorageDirectory()

I am using above mentioned function to get the directory. It works fine for device which don't support sdcard of devices which only supports sdcard and no other storage.

The issue is that i want to access both sdcard and phone storage so that i can show files from both and I don't want to hardcode any path. All i want is the generic solution by which i can access both storages or a generic solution by which i can access the root folder and then navigate other folder which includes phone(emulated/0) and sdcard folder.

There are similar sort of questions but i haven't found the correct answer. All answers includes some hardcoded paths.

Try using cursor

mCursor = MediaStore.Images.Media.query(
            mainCR,
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null,
            null, MediaStore.Images.Media.DATE_MODIFIED + " DESC");

    while (mCursor.moveToNext()) {
        String path = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
        String dateModified = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATE_MODIFIED));
        String size = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE));
        String name = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME));
    }

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