在android开发分享功能的时候遇到图片的位置在system/media/image/里面,或者给了content://com.zhihu.android.provider.image…这样的uri时,无法获取图片路径,请问各位大神有知道如何解决的吗?
1条回答 默认 最新
- 玄冰罡气 2016-12-26 09:14关注
试试这个工具类。
public class FileHelper {
public static String getRealPath(Uri uri, Activity mActivity) {
String realPath = null;if (Build.VERSION.SDK_INT < 11) realPath = FileHelper.getRealPathFromURI_BelowAPI11(mActivity, uri); // SDK >= 11 else realPath = FileHelper.getRealPathFromURI_API11_And_Above(mActivity, uri); return realPath; } public static String getRealPath(String uriString, Activity mActivity) { return FileHelper.getRealPath(Uri.parse(uriString), mActivity); } @SuppressLint("NewApi") public static String getRealPathFromURI_API11_And_Above(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 the 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; } public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) { String[] proj = {MediaStore.Images.Media.DATA}; String result = null; try { Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } catch (Exception e) { result = null; } return result; } 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 (Exception e) { return null; } 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()); } public static boolean isGooglePhotosUri(Uri uri) { return "com.google.android.apps.photos.content".equals(uri.getAuthority()); }
}
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 请问各位,如何在Jetson nano主控板的Ubuntu系统中安装PyQt5
- ¥15 MAC安装佳能LBP2900驱动的网盘提取码
- ¥400 微信停车小程序谁懂的来
- ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
- ¥15 安装ubantu过程中第一个vfat 文件挂载失败
- ¥20 GZ::CTF如何兼容一些靶机?
- ¥15 etcd集群部署问题
- ¥20 谁可以帮我一下问一下各位
- ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
- ¥150 求 《小魔指》街机游戏机整合模拟软件