根据Uri,Cursor没有获取到对应的属性
Android:
背景:调用摄像头,拍摄视频,指定保存的地址,但是返回的Cursor文件,只有名称和大小的属性,没有其他诸如时长,连ID属性都没有
使用
cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
获取时长,会报错:
java.lang.java.lang.illegalargumentexception
如果不指定位置,属性就可以获取,指定位置后,除了名称和大小的属性之前就都无法获得
还请会的大佬指点一下!
-----------------------------------------------------------------------------
时过经年,发现这个问题还在这挂着,时间太久了,附上目前在使用的某方法,由大佬网络提供
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | /* ******************************* Copyright (c)*********************************\ ** ** (c) Copyright 2015, 蒋朋, china, qd. sd ** All Rights Reserved ** ** By() ** ** **-----------------------------------版本信息------------------------------------ ** 版 本: V0.1 ** **------------------------------------------------------------------------------ ********************************End of Head************************************\ */ import android.annotation.TargetApi; import android.content.ContentUris; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.os.Environment; import android.provider.DocumentsContract; import android.provider.MediaStore; /** * 获取图片的路径 */ public class ImageUtil { @TargetApi ( 19 ) public static String getImageAbsolutePath(Context context, Uri imageUri) { if (context == null || imageUri == null ) return null ; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, imageUri)) { if (isExternalStorageDocument(imageUri)) { String docId = DocumentsContract.getDocumentId(imageUri); String[] split = docId.split( ":" ); String type = split[ 0 ]; if ( "primary" .equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[ 1 ]; } } else if (isDownloadsDocument(imageUri)) { String id = DocumentsContract.getDocumentId(imageUri); Uri contentUri = ContentUris.withAppendedId(Uri.parse( "content://downloads/public_downloads" ), Long.valueOf(id)); return getDataColumn(context, contentUri, null , null ); } else if (isMediaDocument(imageUri)) { String docId = DocumentsContract.getDocumentId(imageUri); 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 = MediaStore.Images.Media._ID + "=?" ; String[] selectionArgs = new String[]{split[ 1 ]}; return getDataColumn(context, contentUri, selection, selectionArgs); } } // MediaStore (and general) else if ( "content" .equalsIgnoreCase(imageUri.getScheme())) { // Return the remote address if (isGooglePhotosUri(imageUri)) return imageUri.getLastPathSegment(); return getDataColumn(context, imageUri, null , null ); } // File else if ( "file" .equalsIgnoreCase(imageUri.getScheme())) { return imageUri.getPath(); } return null ; } public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null ; String column = MediaStore.Images.Media.DATA; String[] projection = {column}; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null ); if (cursor != null && cursor.moveToFirst()) { int index = cursor.getColumnIndexOrThrow(column); return cursor.getString(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()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is Google Photos. */ public static boolean isGooglePhotosUri(Uri uri) { return "com.google.android.apps.photos.content" .equals(uri.getAuthority()); } } |
分类:
Android - 问题
标签:
Android 问题
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具