2

I am trying to list all PDF files which are stored in internal and external memory of Android device using ListView. However,I have no idea now, and the only way I can think of is checking file extension. So would you please give me a hint or suggestion? I really appreciate your help.

NEWBIE
  • 29
  • 1
  • 3

2 Answers2

2

Even though this question is from 2011, the topic is still relevant and there's no proper answer here.

So this code below should work on today's Android devices:

String selection = "_data LIKE '%.pdf'"
try (Cursor cursor = getApplicationContext().getContentResolver().query(MediaStore.Files.getContentUri("external"), null, selection, null, "_id DESC")) {
    if (cursor== null || cursor.getCount() <= 0 || !cursor.moveToFirst()) {
        // this means error, or simply no results found
        return;
    }
    do {
        // your logic goes here
    } while (cursor.moveToNext());
}
Yoav Feuerstein
  • 1,925
  • 2
  • 22
  • 53
  • how to get the file name and location inside do while then? – Mayank Kumar Chaudhari Dec 19 '18 at 12:22
  • @m__ you can debug the code, and see in runtime which columns (and their values) you get with that cursor. Specifically, look out for those listed [here](https://developer.android.com/reference/android/provider/MediaStore.Files.FileColumns) and [here](https://developer.android.com/reference/android/provider/MediaStore.MediaColumns). – Yoav Feuerstein Dec 19 '18 at 14:20
1

This might be what you are looking for: MediaStore.Files

Media provider table containing an index of all files in the media storage, including non-media files. This should be used by applications that work with non-media file types (text, HTML, PDF, etc) as well as applications that need to work with multiple media file types in a single query.

mibollma
  • 14,959
  • 6
  • 52
  • 69