List all pdf files in Android Devices [duplicate]

2020-04-18 01:22发布

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.

2条回答
啃猪蹄的小仙女
2楼-- · 2020-04-18 02:01

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());
}
查看更多
萌系小妹纸
3楼-- · 2020-04-18 02:04

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.

查看更多
登录 后发表回答