W
W
Wade2k2020-07-29 15:33:33
Android
Wade2k, 2020-07-29 15:33:33

Why doesn't Mediastore.Files work for pdf files on the emulator?

Kind!

I want to get a list of all pdf files on the device in the pdf viewer application.

I do it with this code:

private fun getPdf() {
        val projection = arrayOf(
                MediaStore.Files.FileColumns._ID,
                MediaStore.Files.FileColumns.MIME_TYPE,
                MediaStore.Files.FileColumns.DATE_ADDED,
                MediaStore.Files.FileColumns.DATE_MODIFIED,
                MediaStore.Files.FileColumns.DISPLAY_NAME,
                MediaStore.Files.FileColumns.TITLE,
                MediaStore.Files.FileColumns.SIZE)

        val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf")
        val whereClause = MediaStore.Files.FileColumns.MIME_TYPE + " IN ('" + mimeType + "')"
        val orderBy = MediaStore.Files.FileColumns.SIZE + " DESC"
        val cursor: Cursor? = contentResolver.query(MediaStore.Files.getContentUri("external"),
                projection,
                whereClause,
                null,
                orderBy)

        val idCol = cursor!!.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID)
        val mimeCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE)
        val addedCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATE_ADDED)
        val modifiedCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATE_MODIFIED)
        val nameCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DISPLAY_NAME)
        val titleCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.TITLE)
        val sizeCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.SIZE)

        if (cursor.moveToFirst()) {
            do {

                val fileUri: Uri = Uri.withAppendedPath(MediaStore.Files.getContentUri("external"), cursor.getString(idCol))
                val mimeType = cursor.getString(mimeCol)
                val dateAdded = cursor.getLong(addedCol)
                val dateModified = cursor.getLong(modifiedCol)
                val nameFile = cursor.getString(nameCol)

                if (nameFile!=null) {

                    mPdfFiles.add(PdfFile(nameFile,fileUri))
                    mPdfAdapter.notifyDataSetChanged()

                }

            } while (cursor.moveToNext())
        }
    }


On real devices, it shows downloaded pdf files, but on the emulator it is empty.
On the emulator, it finds only media files - jpg, png, etc. Pdf does not see.

What can be done?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question