varunkr
varunkr

Reputation: 5552

Get content uri of an audio file from its absolute path

I have gone through a lot of stackoverflow questions but I couldn't find an answer to this. There is an answer for Image files but that doesn't work in my case.

So I have the absolute path of the file using file.getAbsolutePath(). But I need to convert it to contentUri so that below query works fine.

Cursor tempCursor = context.getContentResolver().query(uri,
                    proj, null, null, null);

It is not working. I tried Uri.parse(contentUri) but that doesn't give the content uri I guess. Please help me, I am stuck since a long time. Thanks !!

Upvotes: 0

Views: 1104

Answers (1)

Theo
Theo

Reputation: 2042

You do not use the get.absolute etc. Simply query the Medfiastore.Audio etc and bring back _id and _DATA. _id is the song id and _DATA has the full path and track

below a piece of code which you could use. Just feed it the track name

    public String getThisTrackId(Context context, String trackName) {
    ContentResolver cr = context.getContentResolver();
    final String _id = MediaStore.Audio.Media._ID;
    final String path = MediaStore.Audio.Media.DATA;
    final String[] columns = { _id };
    final String[] trackname = { "%"+ trackName +"%" };
    String where = path + " LIKE ?";
    String strtrack_id  = null;
    Cursor crs =cr.query(uri, columns, where, trackname, null);
    if(crs!= null && crs.moveToFirst()){
        strtrack_id  = crs.getString(crs.getColumnIndexOrThrow(_id));
        crs.close();
    }

    return strtrack_id;
}

Upvotes: 3

Related Questions