fitrahillahs
fitrahillahs

Reputation: 9

MediaStore Uri for External?

As a MediaStore newbie's, I dunno what's wrong with this uri:

MediaStore.Files.getContentUri("external")

Those uri's keeps returns to zero.

My example codes:

String file = null;
String sortBy = "";
if (filterSort == "time") {
    sortBy = MediaStore.Files.FileColumns.DATE_MODIFIED;
} else if (filterSort == "size") {
    sortBy = MediaStore.Files.FileColumns.SIZE;
}
String[] projection = {MediaStore.Files.FileColumns.DATA};
android.database.Cursor cursor = getContentResolver().query(MediaStore.Files.getContentUri("external"), projection, null, null, sortBy + "DESC");
if (cursor != null) {
    while (cursor.moveToNext()) {
        file = cursor.getString(0);
        states.add(file);
    }
    cursor.close();
}

Trying to solve with Uri.parse worked but did not apply sorting filter's. Back with MediaStore.Files.getContentUri("external") , it keeps giving me this via Exception on Android 8.1 devices:

android.database.sqlite.SQLiteException:
no such column: date_modifiedDESC (code 1): , while compiling: SELECT _data FROM files WHERE ( invalid=0) ORDER BY date_modifiedDESC

Is there any way to solve/change/do?

Upvotes: 0

Views: 1639

Answers (1)

Theo
Theo

Reputation: 2042

Nor sure what blackapps is referring to as he does not address the question in any way.

There are a number of uris, depending what you want to extract. for example:

public Uri get_audio_media_uri(){
    Uri uri_to_use = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        uri_to_use = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
    } else {
        uri_to_use = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    }
    return uri_to_use;
}

or for artists;

public Uri get_audio_artist_uri(){
    Uri uri_to_use = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        uri_to_use = MediaStore.Audio.Artists.getContentUri(MediaStore.VOLUME_EXTERNAL);
    } else {
        uri_to_use = MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI;
    }
    return uri_to_use;
}

the part "date_modifiedDESC" is what causes the issue as it requires a space "date_modified DESC"

structure (part) of files table

I suggest you amend your projection, for example

String projection[];
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
    //no _DATA column in Q
    projection = new String[]{
            MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.DURATION,
            MediaStore.Audio.Media.DATE_MODIFIED
    };
} else {
    projection = new String[]{
            MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.DURATION,
            MediaStore.Audio.Media.DATA
            MediaStore.Audio.Media.DATE_MODIFIED
    };
}

instead of MediaStore.Files.FileColumns.DATE_MODIFIED;

Upvotes: 1

Related Questions