Android Uri和文件路徑互相轉(zhuǎn)換的實(shí)例代碼
在項(xiàng)目中需要用到將Uri轉(zhuǎn)換為絕對路徑,在網(wǎng)上找到一個方法,做個筆記
網(wǎng)上有不少方法,但是有的對4.4后的版本無效,這里的方法可以在4.4之后的版本將Uri轉(zhuǎn)換為絕對路徑
public class GetPathFromUri { /** * 專為Android4.4設(shè)計的從Uri獲取文件絕對路徑 */ 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]; } } // 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 getDataColumn(context, uri, null, null); } // File else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return null; } /** * Get the value of the data column for this Uri. This is useful for * MediaStore Uris, and other file-based ContentProviders. * * @param context The context. * @param uri The Uri to query. * @param selection (Optional) Filter used in the query. * @param selectionArgs (Optional) Selection arguments used in the query. * @return The value of the _data column, which is typically a 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 column_index = cursor.getColumnIndexOrThrow(column); return cursor.getString(column_index); } } finally { if (cursor != null) cursor.close(); } return null; } /** * @param uri The Uri to check. * @return Whether the Uri authority is ExternalStorageProvider. */ public static boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is DownloadsProvider. */ public static boolean isDownloadsDocument(Uri uri) { return "com.android.providers.downloads.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is MediaProvider. */ public static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); } }
絕對路徑轉(zhuǎn)Uri比較簡單
以絕對路徑創(chuàng)建一個File對象,然后調(diào)用
Uri.fromFile(file)
以上所述是小編給大家介紹的Android Uri和文件路徑互相轉(zhuǎn)換的實(shí)例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Android開發(fā)必備知識 為什么說Kotlin值得一試
為什么說值得一試,這篇文章主要為大家詳細(xì)介紹了Android開發(fā)必備知識,Kotlin的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05在不同Activity之間傳遞數(shù)據(jù)的四種常用方法
這篇文章主要介紹了在不同Activity之間傳遞數(shù)據(jù)的四種常用方法 的相關(guān)資料,需要的朋友可以參考下2016-03-03Android開發(fā)中Looper.prepare()和Looper.loop()
Looper用于封裝了android線程中的消息循環(huán),默認(rèn)情況下一個線程是不存在消息循環(huán)(message loop)的,具體調(diào)用方法大家可以通過本文學(xué)習(xí)2016-11-11Android中應(yīng)用多進(jìn)程的整理總結(jié)
Android平臺支持多進(jìn)程通信,也支持應(yīng)用內(nèi)實(shí)現(xiàn)多進(jìn)程,下面這篇文章主要給大家介紹了關(guān)于Android中應(yīng)用多進(jìn)程的相關(guān)資料,文中介紹的很詳細(xì),相信對大家具有一定的參考借鑒價值,有需要的朋友們下面來一起看看吧。2017-01-01Android性能調(diào)優(yōu)利器StrictMode應(yīng)用分析
StrictMode意思為嚴(yán)格模式,是用來檢測程序中違例情況的開發(fā)者工具。最常用的場景就是檢測主線程中本地磁盤和網(wǎng)絡(luò)讀寫等耗時的操作。這篇文章給大家介紹Android性能調(diào)優(yōu)利器StrictMode應(yīng)用分析,感興趣的朋友一起看看吧2018-01-01Android 類似UC瀏覽器的效果:向上滑動地址欄隱藏功能
這篇文章主要介紹了Android 類似UC瀏覽器的效果:向上滑動地址欄隱藏功能,需要的朋友可以參考下2017-12-12Android bindService的使用與Service生命周期案例詳解
這篇文章主要介紹了Android bindService的使用與Service生命周期案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09Android自定義View的實(shí)現(xiàn)方法實(shí)例詳解
本文通過實(shí)例代碼給大家詳細(xì)介紹了Android自定義View的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-09-09Android實(shí)現(xiàn)通過手勢控制圖片大小縮放的方法
這篇文章主要介紹了Android實(shí)現(xiàn)通過手勢控制圖片大小縮放的方法,結(jié)合實(shí)例形式分析了Android控制圖片縮放的原理、實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-10-10