android - Get MediaStore content Uri from File path? -
i have application lists videos in mediastore. (i need both internal storage , external storage).
basically have 2 cursors query mediastore.video.media.external_content_uri , mediastore.video.media.internal_content_uri.
then use mergecursor merge these queries , display in listview cursoradapter.
the thing want delete 1 of videos, need "content uri" operation because don't have way determine storage of selected video.
i have full file path of video , id in mediastore.
how can "content uri" file path/uri?
to answer question in title:
i have path uris , uri paths in app. former:
/** * gets corresponding path file given content:// uri * @param selectedvideouri content:// uri find file path * @param contentresolver content resolver use perform query. * @return file path string */ private string getfilepathfromcontenturi(uri selectedvideouri, contentresolver contentresolver) { string filepath; string[] filepathcolumn = {mediacolumns.data}; cursor cursor = contentresolver.query(selectedvideouri, filepathcolumn, null, null, null); cursor.movetofirst(); int columnindex = cursor.getcolumnindex(filepathcolumn[0]); filepath = cursor.getstring(columnindex); cursor.close(); return filepath; }
the latter (which videos, can used audio or files or other types of stored content substituting mediastore.audio (etc) mediastore.video):
/** * gets mediastore video id of given file on external storage * @param filepath path (on external storage) of file resolve id of * @param contentresolver content resolver use perform query. * @return video id long */ private long getvideoidfromfilepath(string filepath, contentresolver contentresolver) { long videoid; log.d(tag,"loading file " + filepath); // returns content://media/external/videos/media (or that) // pass in "external" because that's mediastore's name external // storage on device (the other possibility "internal") uri videosuri = mediastore.video.media.getcontenturi("external"); log.d(tag,"videosuri = " + videosuri.tostring()); string[] projection = {mediastore.video.videocolumns._id}; // todo break if have no matching item in mediastore. cursor cursor = contentresolver.query(videosuri, projection, mediastore.video.videocolumns.data + " ?", new string[] { filepath }, null); cursor.movetofirst(); int columnindex = cursor.getcolumnindex(projection[0]); videoid = cursor.getlong(columnindex); log.d(tag,"video id " + videoid); cursor.close(); return videoid; }
basically, data
column of mediastore
(or whichever sub-section of you're querying) stores file path, use info up.
Comments
Post a Comment