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

您的位置:首頁技術文章
文章詳情頁

Android7.0以上Uri轉路徑的方法實現(已驗證)

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

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

解決方法

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

代碼如下:

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;}

附上全系統的代碼:

/** * 文件Uri轉路徑(兼容各品牌手機) */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的數據列的值。這對于MediaStore uri和其他基于文件的內容提供程序非常有用。 */ 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

另發現一篇,親測,Android 4.4到Android 10可用,測試的系統有VIVO、OPPO、MIUI、EMUI...

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

直接可用的代碼片段:

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)) { //下載內容提供者時應當判斷下載管理器是否被禁用 int stateCode = context.getPackageManager().getApplicationEnabledSetting('com.android.providers.downloads'); if (stateCode != 0 && stateCode != 1) { return null; } String id = DocumentsContract.getDocumentId(uri); // 如果出現這個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()); }}

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

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

標簽: Android
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
日韩不卡一二三区| 99视频一区| 亚洲欧洲日本mm| 国产欧洲在线| 国产精品天天看天天狠| 乱人伦精品视频在线观看| 亚洲va中文在线播放免费| 久久爱www成人| 日韩三区四区| 美女久久网站| 一区三区视频| 国产精品av一区二区| 夜鲁夜鲁夜鲁视频在线播放| 久久精品国产精品亚洲毛片| 日韩激情一二三区| 91精品啪在线观看国产18| 国产一区2区在线观看| 国产精品美女久久久久久不卡| 亚洲精品伊人| 免费成人av在线播放| 色爱av综合网| 久久久精品网| 久久精品亚洲欧美日韩精品中文字幕| 国产aⅴ精品一区二区三区久久| 老鸭窝一区二区久久精品| 国产毛片久久久| 欧美日韩1区| 久久国产尿小便嘘嘘| 日韩福利视频网| 日韩高清电影免费| 一二三区精品| 亚洲91在线| 日本a级不卡| 国产欧美精品久久| 久久爱www.| 国产一区国产二区国产三区| 久久91视频| 新版的欧美在线视频| 精品捆绑调教一区二区三区| 999国产精品视频| 九九在线精品| 美女精品网站| 日韩成人午夜精品| 国产精品久久久久久久久久妞妞| 国产精品久久久久久久久久齐齐| 日韩不卡一二三区| 国产日韩高清一区二区三区在线 | 黄色网一区二区| 国产在线视频欧美一区| 国产福利资源一区| 国产日产一区| 成人污污视频| 99久久久久久中文字幕一区| 亚洲激情久久| 婷婷亚洲成人| 免费一级欧美在线观看视频| 日本一区二区免费高清| 成人精品视频| 午夜国产精品视频免费体验区| 99国产精品久久久久久久| 亚洲v天堂v手机在线| 久久av免费| 日韩国产一区二区| 亚洲综合日韩| 国产一精品一av一免费爽爽| 国模大尺度视频一区二区| 超级白嫩亚洲国产第一| 亚洲免费激情| 视频一区在线播放| 国产精品麻豆成人av电影艾秋 | 久久婷婷亚洲| 伊人成人在线视频| 欧美精品一卡| 日韩三区四区| 中文在线免费视频| 日本欧美在线看| 精品一区二区三区在线观看视频| 亚洲高清不卡| 欧美影院视频| 四虎4545www国产精品| 亚洲精品乱码日韩| 国产成人免费视频网站视频社区| 久久精品主播| 日韩精品视频中文字幕| 亚洲免费福利| 亚洲资源网站| 成人精品国产亚洲| 日韩在线播放一区二区| 九九九精品视频| 黄色国产精品| 国产精品一区二区av日韩在线| 日韩精品欧美| 日韩高清二区| 欧美午夜精彩| 久久精品一本| 亚洲精品自拍| 国产字幕视频一区二区| 日本不卡一区二区| 日本精品影院| 欧美激情在线精品一区二区三区| 夜夜嗨网站十八久久| 国产成人免费精品| 日韩av在线免费观看不卡| 欧美日韩中文一区二区| 欧美激情麻豆| 亚洲一二av| 欧美丝袜一区| 久久久久久久久成人| 亚洲影视一区| 欧美精品一区二区三区精品| 你懂的网址国产 欧美| 免费在线看一区| 久久精品女人| 日韩精彩视频在线观看| 99视频一区| 五月激情久久| 精品久久99| 久久精品 人人爱| 综合色一区二区| 九一国产精品| | 美国三级日本三级久久99| 免费在线小视频| 欧美国产精品| 日韩不卡一区二区三区| 欧美成人基地 | 美国三级日本三级久久99 | 亚洲免费观看| 久久精选视频| 精品午夜视频| 国产日韩欧美一区二区三区 | 成人av二区| 国产美女高潮在线| 麻豆成人综合网| 日韩av中文在线观看| 日韩影院免费视频| 夜久久久久久| 91精品二区| 日韩精品91| 国产一区二区三区不卡视频网站| 91精品国产自产在线丝袜啪| 免费中文字幕日韩欧美| 欧美日韩国产一区二区三区不卡| 欧美二三四区| 蜜桃成人精品| 91精品一区国产高清在线gif| 青青青免费在线视频| 国产成人1区| 狠狠久久伊人中文字幕| 麻豆成人综合网| 美女毛片一区二区三区四区最新中文字幕亚洲 | 国产精品一站二站| 麻豆中文一区二区| 日韩欧美午夜| 99亚洲精品| 日本成人精品| 欧美韩日一区| 99re国产精品| 911精品国产| 亚洲最新无码中文字幕久久| 亚洲福利一区| 亚洲精品高潮| 老牛国内精品亚洲成av人片| аⅴ资源天堂资源库在线| 亚洲欧美日韩高清在线| 少妇精品久久久一区二区| 麻豆精品99| 九九综合在线| 国产精品一区二区美女视频免费看 | 国产精品天堂蜜av在线播放| 成人综合一区| 三级欧美韩日大片在线看| 国产精品成人**免费视频 | 日韩不卡一二三区| 中国字幕a在线看韩国电影| 久久国产精品久久w女人spa| 久久国内精品自在自线400部| 久草免费在线视频| 午夜久久影院| 国产免费av一区二区三区| 久久久久久久久丰满| 亚洲精一区二区三区| 国产精品蜜芽在线观看| 日韩精品中文字幕第1页| 亚洲精品少妇| 日韩国产激情| 日韩一二三区在线观看| 理论片午夜视频在线观看| 爽好久久久欧美精品| 国产suv精品一区| 亚洲免费一区二区| 风间由美中文字幕在线看视频国产欧美| 2023国产精品久久久精品双| 日韩精品亚洲专区在线观看| 亚洲成人不卡| 国产乱码精品一区二区三区亚洲人| 欧美成a人免费观看久久| 日韩精品视频网| 欧美91精品| 精品国产午夜肉伦伦影院 |