0

I am creating (trying) a file manager app and i want to show recent files (all types) on main activity. I tried many answers that only shows how to get only one type of media files. Is there any way to get all type of recent media files.

I tried to modify answers i found online but it's giving null as display name.

val projection = arrayOf(
    MediaStore.Files.FileColumns._ID,
    MediaStore.Files.FileColumns.DISPLAY_NAME,
    MediaStore.Files.FileColumns.DATE_ADDED,
    MediaStore.Files.FileColumns.SIZE
)
val sortOrder = "${MediaStore.Files.FileColumns.DATE_ADDED} ASC limit 10"
applicationContext.contentResolver.query(
    MediaStore.Files.getContentUri(MediaStore.VOLUME_INTERNAL),
    projection,
    null,
    null,
    sortOrder
)?.use { cursor ->
    Log.e("TAG", ">> " + cursor.count)
    val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID)
    val nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DISPLAY_NAME)
    while (cursor.moveToNext()) {
        Log.e("TAG", ">> " + cursor.getLong(idColumn))
        Log.e("TAG", ">> " + cursor.getString(nameColumn))
    }
}
Porush Manjhi
  • 135
  • 12
  • Yes. You can. Create an intent with all necessary mimetypes clubbed – MdBasha Aug 26 '20 at 08:07
  • Check this [link](https://stackoverflow.com/a/63485060/14129632) – MdBasha Aug 26 '20 at 08:07
  • Hi @MdBasha, I don't want to pick a file instead i want recent files list like how we do it with mediastore. – Porush Manjhi Aug 26 '20 at 08:15
  • Your question says you have solution for showing one type of recent media file. Get that code modified with the mimetypes to display all types or produce a minimum reproducible code – MdBasha Aug 26 '20 at 08:31
  • Hi @MdBasha, Check my updated question. – Porush Manjhi Aug 26 '20 at 08:51
  • Yes, I see it. Please have a look at this [answer](https://stackoverflow.com/a/39356039/13031115) for retrieving all the filelist from Mediastore.File. Modify the array to (Mediastore.File directory) to suit your requirement – MdBasha Aug 26 '20 at 09:23
  • In Manifest add permissions ` ` – MdBasha Aug 26 '20 at 09:31
  • Hi @MdBasha, My code is already a modified version of code i found on online but it's not working. Check the comments of answer you shared there is no reply for file media type. – Porush Manjhi Aug 26 '20 at 09:44

1 Answers1

0

Try

private void Recentfilelist() {
        try {
            

            Uri internalUri= MediaStore.Files.getContentUri(MediaStore.VOLUME_INTERNAL):


            projection=new String[]{MediaStore.Files.FileColumns._ID,
                    MediaStore.Files.FileColumns.TITLE,
                    MediaStore.Files.FileColumns.DATE_ADDED,
                    MediaStore.Files.FileColumns.SIZE};

            String selection =null;

            String[] selectionArgs = null;

            String sortOrder = MediaStore.Audio.Media.DATE_ADDED + " ASC";

            Cursor recentfilesCursor = getContentResolver().query(internalUri, projection, selection, selectionArgs, sortOrder);

            if (recentfilesCursor != null) {
                if (recentfilesCursor.moveToFirst()) {
                    do {
                        int filetitle = recentfilesCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
                        int file_id = recentfilesCursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
                        int filePath = recentfilesCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
                        Mediafileinfo info = new Mediafileinfo();
                        info.setData(new File(new File(recentfilesCursor.getString(filedata)).getParent()).getName());
                        info.setTitle(recentfilesCursor.getString(filetitle));
                        info.set_id(recentfilesCursor.getString(file_id));
                        mediaList.add(info);
                    } while (recentfilesCursor.moveToNext());
                }
            }
            assert recentfilesCursor != null;
            recentfilesCursor.close();
} catch (Exception e) {
                e.printStackTrace();
            }
        }
MdBasha
  • 423
  • 4
  • 16
  • Replace DISPLAY_NAME with TITLE. This code is for File type. For the Audio, video, images use the previous [link](https://stackoverflow.com/questions/39354275/how-to-get-data-from-mediastore-file-and-mediastore-file-filecolumn/39356039#39356039) shared and all these update a common medialist array. Populate the array values in your mainactivity. That's all. – MdBasha Aug 26 '20 at 10:33
  • Hi @MdBasha, I will try it today and inform you what happens. – Porush Manjhi Aug 27 '20 at 02:46
  • Hi @MdBasha, Now it's showing names but I don't recognize file names probably system files but it's not showing recent files like other file manager does. – Porush Manjhi Aug 27 '20 at 13:13
  • Hi Porush, I apologize for the late reply. Did you solve it ? – MdBasha Sep 01 '20 at 12:32
  • Hi @MdBasha. No, I am still stuck and so putting this project on hold until I find a working solution. – Porush Manjhi Sep 06 '20 at 11:18