Mediastore - Uri to Query All Types of Files (Media and Non-Media)

MediaStore - Uri to query all types of files (media and non-media)

It is "external" or "internal" although internal (system files) is probably not useful here.

ContentResolver cr = context.getContentResolver();
Uri uri = MediaStore.Files.getContentUri("external");

// every column, although that is huge waste, you probably need
// BaseColumns.DATA (the path) only.
String[] projection = null;

// exclude media files, they would be here also.
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_NONE;
String[] selectionArgs = null; // there is no ? in selection so null here

String sortOrder = null; // unordered
Cursor allNonMediaFiles = cr.query(uri, projection, selection, selectionArgs, sortOrder);

If you want .pdf only you could check the mimetype

// only pdf
String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
String[] selectionArgsPdf = new String[]{ mimeType };
Cursor allPdfFiles = cr.query(uri, projection, selectionMimeType, selectionArgsPdf, sortOrder);

How to get data from MediaStore.File and MediaStore.File.FileColumn

Writing a fresh answer, since all you want is the folder names of the audio files.

So the best thing to use here is MediaStore.Audio.Media instead of using MediaStore.File. Get the folder names using the below on the audio file path.

new File(new File(audioCursor.getString(filedata)).getParent()).getName()

private void External() {
try {
Uri externalUri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

projection=new String[]{MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,;

String selection =null;

String[] selectionArgs = null;

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

Cursor audioCursor = getContentResolver().query(externalUri, proj, selection, selectionArgs, sortOrder);

if (audioCursor != null) {
if (audioCursor.moveToFirst()) {
do {
int filetitle = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
int file_id = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
int filePath = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
Mediafileinfo info = new Mediafileinfo();
info.setData(new File(new File(audioCursor.getString(filedata)).getParent()).getName());
info.setTitle(audioCursor.getString(filetitle));
info.set_id(audioCursor.getString(file_id));
audioList.add(info);
} while (audioCursor.moveToNext());
}
}
assert audioCursor != null;
audioCursor.close();
} catch (Exception e) {
e.printStackTrace();
}
}

MediaStore cursor not displaying non-media files in Android 10

Since Android 10 you cannot use MediaStore to load files other than media (audio, video, image).

The official way to open a document is to use ACTION_OPEN_DOCUMENT and let a user select a file from the file picker.

Check the docs:
https://developer.android.com/training/data-storage/shared/media#other-file-types

How can i get the uri of non media type and its id in android?

I'm sure you are going to find that you are looking for in the answer to this question: MediaStore - Uri to query all types of files (media and non-media)



Related Topics



Leave a reply



Submit