日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術(shù)文章
文章詳情頁

Android7.0以上Uri轉(zhuǎn)路徑的方法實現(xiàn)(已驗證)

瀏覽:151日期:2022-09-27 08:09:09

網(wǎng)絡(luò)上看到過很多種Uri轉(zhuǎn)路徑的方法,可基本上都只適用于很少的Uri值,可能沒有結(jié)果(例如,對于由MediaStore索引的非本地文件),也可能沒有可用的結(jié)果(例如,對于可移動存儲上的文件)。

解決方法

使用ContentResolver和openInputStream()在Uri標識的內(nèi)容上獲取InputStream。在控制的文件上使用InputStream和FileOutputStream復(fù)制內(nèi)容,然后使用該文件。

代碼如下:

private static String getFilePathForN(Context context, Uri uri) { try { Cursor returnCursor = context.getContentResolver().query(uri, null, null, null, null); int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); returnCursor.moveToFirst(); String name = (returnCursor.getString(nameIndex)); File file = new File(context.getFilesDir(), name); InputStream inputStream = context.getContentResolver().openInputStream(uri); FileOutputStream outputStream = new FileOutputStream(file); int read = 0; int maxBufferSize = 1 * 1024 * 1024; int bytesAvailable = inputStream.available(); int bufferSize = Math.min(bytesAvailable, maxBufferSize); final byte[] buffers = new byte[bufferSize]; while ((read = inputStream.read(buffers)) != -1) { outputStream.write(buffers, 0, read); } returnCursor.close(); inputStream.close(); outputStream.close(); return file.getPath(); } catch (Exception e) { e.printStackTrace(); } return null;}

附上全系統(tǒng)的代碼:

/** * 文件Uri轉(zhuǎn)路徑(兼容各品牌手機) */public class PathUtils { /** * android7.0以上處理方法 */ private static String getFilePathForN(Context context, Uri uri) { try { Cursor returnCursor = context.getContentResolver().query(uri, null, null, null, null); int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); returnCursor.moveToFirst(); String name = (returnCursor.getString(nameIndex)); File file = new File(context.getFilesDir(), name); InputStream inputStream = context.getContentResolver().openInputStream(uri); FileOutputStream outputStream = new FileOutputStream(file); int read = 0; int maxBufferSize = 1 * 1024 * 1024; int bytesAvailable = inputStream.available(); int bufferSize = Math.min(bytesAvailable, maxBufferSize); final byte[] buffers = new byte[bufferSize]; while ((read = inputStream.read(buffers)) != -1) {outputStream.write(buffers, 0, read); } returnCursor.close(); inputStream.close(); outputStream.close(); return file.getPath(); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 全平臺處理方法 */ public static String getPath(final Context context, final Uri uri) throws Exception { final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; final boolean isN = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N; if (isN) { return getFilePathForN(context, uri); } // 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'), StringUtils.toLong(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; } /** * 獲取此Uri的數(shù)據(jù)列的值。這對于MediaStore uri和其他基于文件的內(nèi)容提供程序非常有用。 */ 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); } } catch (IllegalArgumentException e){ //do nothing } finally { if (cursor != null)cursor.close(); } return null; } public static boolean isExternalStorageDocument(Uri uri) { return 'com.android.externalstorage.documents'.equals(uri.getAuthority()); } public static boolean isDownloadsDocument(Uri uri) { return 'com.android.providers.downloads.documents'.equals(uri.getAuthority()); } public static boolean isMediaDocument(Uri uri) { return 'com.android.providers.media.documents'.equals(uri.getAuthority()); }}

參考資料:https://stackoverflow.com/questions/42508383/illegalargumentexception-column-data-does-not-exist

另發(fā)現(xiàn)一篇,親測,Android 4.4到Android 10可用,測試的系統(tǒng)有VIVO、OPPO、MIUI、EMUI...

解決的國內(nèi)產(chǎn)商問題:華為的黃色圖標管理器,他返回了4.4的標準的Uri了,不是4.4以上的標準的Uri,導(dǎo)致解析的時候,判斷到版本 > 4.4,然后用了4.4以上的標準的解析,然后失敗了,并非不回調(diào)。

直接可用的代碼片段:

public class FileUtils { private Context context; public FileUtils(Context context) { this.context = context; } public String getFilePathByUri(Uri uri) { // 以 file:// 開頭的 if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) { return uri.getPath(); } // 以/storage開頭的也直接返回 if (isOtherDocument(uri)) { return uri.getPath(); } // 版本兼容的獲取! String path = getFilePathByUri_BELOWAPI11(uri); if (path != null) { LogUtils.d('getFilePathByUri_BELOWAPI11獲取到的路徑為:' + path); return path; } path = getFilePathByUri_API11to18(uri); if (path != null) { LogUtils.d('getFilePathByUri_API11to18獲取到的路徑為:' + path); return path; } path = getFilePathByUri_API19(uri); LogUtils.d('getFilePathByUri_API19獲取到的路徑為:' + path); return path; } private String getFilePathByUri_BELOWAPI11(Uri uri) { // 以 content:// 開頭的,比如 content://media/extenral/images/media/17766 if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { String path = null; String[] projection = new String[]{MediaStore.Images.Media.DATA}; Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null); if (cursor != null) {if (cursor.moveToFirst()) { int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); if (columnIndex > -1) { path = cursor.getString(columnIndex); }}cursor.close(); } return path; } return null; } private String getFilePathByUri_API11to18(Uri contentUri) { String[] projection = {MediaStore.Images.Media.DATA}; String result = null; CursorLoader cursorLoader = new CursorLoader(context, contentUri, projection, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); cursor.close(); } return result; } private String getFilePathByUri_API19(Uri uri) { // 4.4及之后的 是以 content:// 開頭的,比如 content://com.android.providers.media.documents/document/image%3A235700 if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (DocumentsContract.isDocumentUri(context, uri)) {if (isExternalStorageDocument(uri)) { // ExternalStorageProvider String docId = DocumentsContract.getDocumentId(uri); String[] split = docId.split(':'); String type = split[0]; if ('primary'.equalsIgnoreCase(type)) { if (split.length > 1) { return Environment.getExternalStorageDirectory() + '/' + split[1]; } else { return Environment.getExternalStorageDirectory() + '/'; } // This is for checking SD Card }} else if (isDownloadsDocument(uri)) { //下載內(nèi)容提供者時應(yīng)當判斷下載管理器是否被禁用 int stateCode = context.getPackageManager().getApplicationEnabledSetting('com.android.providers.downloads'); if (stateCode != 0 && stateCode != 1) { return null; } String id = DocumentsContract.getDocumentId(uri); // 如果出現(xiàn)這個RAW地址,我們則可以直接返回! if (id.startsWith('raw:')) { return id.replaceFirst('raw:', ''); } if (id.contains(':')) { String[] tmp = id.split(':'); if (tmp.length > 1) { id = tmp[1]; } } Uri contentUri = Uri.parse('content://downloads/public_downloads'); LogUtils.d('測試打印Uri: ' + uri); try { contentUri = ContentUris.withAppendedId(contentUri, Long.parseLong(id)); } catch (Exception e) { e.printStackTrace(); } String path = getDataColumn(contentUri, null, null); if (path != null) return path; // 兼容某些特殊情況下的文件管理器! String fileName = getFileNameByUri(uri); if (fileName != null) { path = Environment.getExternalStorageDirectory().toString() + '/Download/' + fileName; return path; }} else if (isMediaDocument(uri)) { // MediaProvider String docId = DocumentsContract.getDocumentId(uri); String[] split = docId.split(':'); 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; } String selection = '_id=?'; String[] selectionArgs = new String[]{split[1]}; return getDataColumn(contentUri, selection, selectionArgs);} } } return null; } private String getFileNameByUri(Uri uri) { String relativePath = getFileRelativePathByUri_API18(uri); if (relativePath == null) relativePath = ''; final String[] projection = {MediaStore.MediaColumns.DISPLAY_NAME }; try (Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null)) { if (cursor != null && cursor.moveToFirst()) {int index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME);return relativePath + cursor.getString(index); } } return null; } private String getFileRelativePathByUri_API18(Uri uri) { final String[] projection; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) { projection = new String[]{ MediaStore.MediaColumns.RELATIVE_PATH }; try (Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null)) {if (cursor != null && cursor.moveToFirst()) { int index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.RELATIVE_PATH); return cursor.getString(index);} } } return null; } private String getDataColumn(Uri uri, String selection, String[] selectionArgs) { final String column = MediaStore.Images.Media.DATA; final String[] projection = {column}; try (Cursor 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); } } catch (IllegalArgumentException iae) { iae.printStackTrace(); } return null; } private boolean isExternalStorageDocument(Uri uri) { return 'com.android.externalstorage.documents'.equals(uri.getAuthority()); } private boolean isOtherDocument(Uri uri) { // 以/storage開頭的也直接返回 if (uri != null && uri.getPath() != null) { String path = uri.getPath(); if (path.startsWith('/storage')) {return true; } if (path.startsWith('/external_files')) {return true; } } return false; } private boolean isDownloadsDocument(Uri uri) { return 'com.android.providers.downloads.documents'.equals(uri.getAuthority()); } private boolean isMediaDocument(Uri uri) { return 'com.android.providers.media.documents'.equals(uri.getAuthority()); }}

調(diào)用 getFilePathByUri(Uri uri) 即可獲得最終的路徑。

到此這篇關(guān)于Android7.0以上Uri轉(zhuǎn)路徑的方法實現(xiàn)(已驗證)的文章就介紹到這了,更多相關(guān)Android7 Uri轉(zhuǎn)路徑內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Android
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
亚洲精品一级| 欧美精品91| 精品日韩视频| 一本大道色婷婷在线| 国产一区2区| 色爱综合网欧美| 五月激情久久| 久久精品不卡| 国产视频久久| 亚洲专区视频| 三级久久三级久久久| 91精品国产自产观看在线 | 日韩欧美自拍| 精品视频网站| 亚洲永久av| 国精品一区二区| 蜜臀久久久99精品久久久久久| 亚洲一区二区免费在线观看| 日韩高清欧美激情| 麻豆精品久久久| 日韩一区二区三区在线免费观看| 日本精品不卡| 亚洲综合精品| 91亚洲精品在看在线观看高清| 国产福利一区二区精品秒拍 | 日韩高清中文字幕一区| 国产精品玖玖玖在线资源| 麻豆成人av在线| 韩国久久久久久| 亚洲精品91| 日韩一区精品| 日本久久黄色| 麻豆亚洲精品| 国产情侣一区| 日韩欧美一区二区三区免费观看| 欧美大黑bbbbbbbbb在线| 亚洲久久一区| 国内不卡的一区二区三区中文字幕| 国产亚洲一区二区手机在线观看| 亚洲欧美不卡| 老司机免费视频一区二区三区| 欧美gv在线| 玖玖玖国产精品| 里番精品3d一二三区| 欧美1区2区3区| 91在线成人| 久久亚洲国产| 日韩精品免费视频人成| 91日韩在线| 亚洲精一区二区三区| 精品国产一区二区三区2021| 中文国产一区| 麻豆久久久久久| 伊人成人在线视频| 国产精品一区三区在线观看| 久久久久久久久丰满| 日本不卡视频一二三区| 精品72久久久久中文字幕| 国产一级一区二区| 国产成人精品一区二区三区视频 | 麻豆mv在线观看| 综合在线一区| 欧美二三四区| 国产欧美日韩一级| 午夜av一区| 国产精品va| 性欧美长视频| 中文在线а√天堂| 国产欧美激情| 在线一区免费观看| 国产精选在线| 国产精品亚洲四区在线观看| 黄色av日韩| 国产精品福利在线观看播放| 91亚洲精品在看在线观看高清| 九九综合九九| 高清久久精品| 青草av.久久免费一区| 婷婷成人基地| 麻豆视频在线观看免费网站黄| 日韩一区二区三区四区五区| 国内亚洲精品| 国产一区二区三区四区五区传媒| 亚洲69av| 亚洲自拍另类| 激情欧美丁香| 特黄毛片在线观看| 精品久久亚洲| 国产精品免费大片| 亚洲精品在线a| 国产亚洲精品自拍| 日韩不卡视频在线观看| 久久久久久亚洲精品美女| 日韩欧美在线精品| 美女精品网站| 日韩午夜av在线| 久久精选视频| av最新在线| 精品美女在线视频| 久久久精品国产**网站| 国产日韩一区| 国产日韩一区二区三免费高清| 综合亚洲视频| 免费观看久久久4p| 亚洲欧美视频| 亚洲成人精选| 欧美日韩国产在线观看网站| 久久蜜桃精品| 亚洲91视频| 欧美成人国产| 欧美综合另类| 久久精品国产68国产精品亚洲| 日韩福利一区| 日韩国产一区| 九色porny丨国产首页在线| а√在线中文在线新版| 天堂√8在线中文| 久久国产毛片| 美女久久久久| 中文日韩欧美| 亚洲精品看片| 日韩精品导航| 国产精选一区| 国产精品网址| 久久av中文| 久久av资源| 国产一区二区三区四区大秀 | 欧美国产精品| 久久香蕉精品香蕉| 国产成人精品一区二区三区在线| 久久av免费| 成人国产综合| 天堂日韩电影| 夜久久久久久| 日韩有码av| 久久99影视| 91亚洲国产| 亚洲国产专区校园欧美| 欧美日韩精品一本二本三本| 91精品二区| 亚洲永久精品唐人导航网址| 日韩激情视频网站| 国产精品a久久久久| 日韩av自拍| 在线视频观看日韩| 在线国产日韩| 美女在线视频一区| 亚洲1234区| 欧美精品九九| 亚洲精品高潮| 久久精品五月| 国产综合欧美| 亚洲精品视频一二三区| 日韩成人午夜精品| 国产精品.xx视频.xxtv| 日本免费一区二区三区四区| 视频一区中文| 日韩福利视频网| 国产乱码午夜在线视频| 亚洲欧洲午夜| 国产免费播放一区二区| 超碰成人av| 日韩精品一级二级| 欧美精品第一区| 欧美日韩在线观看视频小说| 综合欧美精品| 高清日韩中文字幕| 狠狠爱成人网| 国产欧美久久一区二区三区| 97精品在线| 久久亚洲风情| 精品三区视频| 视频精品一区二区| 久久精品资源| 国产精品日韩精品在线播放| 欧洲在线一区| 777久久精品| 亚洲爱爱视频| 91国内精品| 99精品在线| 欧美视频二区| 亚洲天堂黄色| 国产精品扒开腿做爽爽爽软件| 婷婷色综合网| 国产福利资源一区| 女主播福利一区| 国产精品久久777777毛茸茸| 久久国产电影| 国产日韩亚洲| 欧美特黄一级| 久久伊人国产| 视频一区中文字幕| 国产成人1区| 亚洲不卡视频| 国产综合亚洲精品一区二| 国产精品黄色| 丝袜脚交一区二区| 伊人久久高清| 国产一区二区三区亚洲综合| 亚洲一区激情|