Android file chooser redirects audio files to /external -
i writing app requires user choose files of various types on phone. use code launch file chooser intent:
intent intent = new intent(intent.action_get_content); intent.settype("file/*"); startactivityforresult(intent.createchooser(intent, "unhelpful text"), integer_constant);
and here relevant part of onactivityresult:
if (requestcode == integer_constant && resultcode == result_ok) { uri uri = data.getdata(); path = uri.getpath(); log.i(debug_tag, "path: " + path); }
when choose pdf file or video on sd card, path displayed log should be: /storage/emulated/path/to/file
. when choose audio file in wav format in same folder document or video (or other folder), path printed log /external/audio/media/217
.
why happen, , how can retrieve actual path of file?
/** * file path uri. the path storage access * framework documents, _data field mediastore , * other file-based contentproviders. * * @param context context. * @param uri uri query. * @author paulburke */ public static string getpath(final context context, final uri uri) { final boolean iskitkat = build.version.sdk_int >= build.version_codes.kitkat; // documentprovider if (iskitkat && documentscontract.isdocumenturi(context, uri)) { // externalstorageprovider if (isexternalstoragedocument(uri)) { final string docid = documentscontract.getdocumentid(uri); final string[] split = docid.split(":"); final string type = split[0]; if ("primary".equalsignorecase(type)) { return environment.getexternalstoragedirectory() + "/" + split[1]; } // todo handle non-primary volumes } // downloadsprovider else if (isdownloadsdocument(uri)) { final string id = documentscontract.getdocumentid(uri); final uri contenturi = contenturis.withappendedid( uri.parse("content://downloads/public_downloads"), long.valueof(id)); return getdatacolumn(context, contenturi, null, null); } // mediaprovider else if (ismediadocument(uri)) { final string docid = documentscontract.getdocumentid(uri); final string[] split = docid.split(":"); final string type = split[0]; uri contenturi = null; if ("image".equals(type)) { contenturi = mediastore.images.media.external_content_uri; } else if ("video".equals(type)) { contenturi = mediastore.video.media.external_content_uri; } else if ("audio".equals(type)) { contenturi = mediastore.audio.media.external_content_uri; } final string selection = "_id=?"; final string[] selectionargs = new string[] { split[1] }; return getdatacolumn(context, contenturi, selection, selectionargs); } } // mediastore (and general) else if ("content".equalsignorecase(uri.getscheme())) { // return remote address if (isgooglephotosuri(uri)) return uri.getlastpathsegment(); return getdatacolumn(context, uri, null, null); } // file else if ("file".equalsignorecase(uri.getscheme())) { return uri.getpath(); } return null; } /** * value of data column uri. useful * mediastore uris, , other file-based contentproviders. * * @param context context. * @param uri uri query. * @param selection (optional) filter used in query. * @param selectionargs (optional) selection arguments used in query. * @return value of _data column, typically file path. */ public static string getdatacolumn(context context, uri uri, string selection, string[] selectionargs) { cursor cursor = null; final string column = "_data"; final string[] projection = { column }; try { cursor = context.getcontentresolver().query(uri, projection, selection, selectionargs, null); if (cursor != null && cursor.movetofirst()) { final int index = cursor.getcolumnindexorthrow(column); return cursor.getstring(index); } } { if (cursor != null) cursor.close(); } return null; } /** * @param uri uri check. * @return whether uri authority externalstorageprovider. */ public static boolean isexternalstoragedocument(uri uri) { return "com.android.externalstorage.documents".equals(uri.getauthority()); } /** * @param uri uri check. * @return whether uri authority downloadsprovider. */ public static boolean isdownloadsdocument(uri uri) { return "com.android.providers.downloads.documents".equals(uri.getauthority()); } /** * @param uri uri check. * @return whether uri authority mediaprovider. */ public static boolean ismediadocument(uri uri) { return "com.android.providers.media.documents".equals(uri.getauthority()); } /** * @param uri uri check. * @return whether uri authority google photos. */ public static boolean isgooglephotosuri(uri uri) { return "com.google.android.apps.photos.content".equals(uri.getauthority()); }
taken https://github.com/ipaulpro/afilechooser/blob/master/afilechooser/src/com/ipaulpro/afilechooser/utils/fileutils.java , answer https://stackoverflow.com/a/20559418/4128945
Comments
Post a Comment