1 package com.netease.nimlib.sdk.util;
3 import android.content.ContentResolver;
4 import android.content.Context;
5 import android.database.Cursor;
6 import android.graphics.Bitmap;
7 import android.media.MediaMetadataRetriever;
8 import android.net.Uri;
9 import android.provider.MediaStore;
10 import android.provider.OpenableColumns;
11 import android.text.TextUtils;
12 import android.webkit.MimeTypeMap;
17 private static final String TAG =
"UriUtils";
21 if(TextUtils.isEmpty(uriString))
27 uri = Uri.parse(uriString);
28 }
catch (Throwable e) {
30 NimLog.e(TAG,
"stringToUri failed,uriString = "+uriString,e);
42 if(TextUtils.isEmpty(uriString))
48 Uri uri = Uri.parse(uriString);
49 isFileUri = ContentResolver.SCHEME_FILE.equals(uri.getScheme());
50 }
catch (Throwable e) {
52 NimLog.e(TAG,
"File URI not valid",e);
68 return ContentResolver.SCHEME_FILE.equals(uri.getScheme());
78 if(TextUtils.isEmpty(uriString))
84 Uri uri = Uri.parse(uriString);
85 isContentUri = ContentResolver.SCHEME_CONTENT.equals(uri.getScheme());
86 }
catch (Throwable e) {
88 NimLog.e(TAG,
"Content URI not valid",e);
104 return ContentResolver.SCHEME_CONTENT.equals(uri.getScheme());
116 if(TextUtils.isEmpty(uriString))
123 Uri uri = Uri.parse(uriString);
124 isFileUri = ContentResolver.SCHEME_FILE.equals(uri.getScheme());
125 isContentUri = ContentResolver.SCHEME_CONTENT.equals(uri.getScheme());
126 }
catch (Throwable e) {
128 NimLog.e(TAG,
"Content URI not valid",e);
149 isFileUri = ContentResolver.SCHEME_FILE.equals(uri.getScheme());
150 isContentUri = ContentResolver.SCHEME_CONTENT.equals(uri.getScheme());
161 if(context == null || uri == null)
165 ContentResolver contentResolver = context.getContentResolver();
166 MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
168 String mimeType = contentResolver.getType(uri);
170 String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
171 if (extension == null) {
172 String path = uri.getPath();
174 extension = MimeTypeMap.getFileExtensionFromUrl(path);
177 if(TextUtils.isEmpty(extension))
191 if(context == null || uri == null)
195 Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
196 int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
198 if (cursor.moveToFirst()) {
200 size = cursor.getLong(sizeIndex);
214 if(context == null || uri == null)
218 String[] projection = {MediaStore.Images.Media.DATA};
219 Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
221 if (cursor != null && cursor.moveToFirst()) {
226 if (cursor != null) {
241 if(context == null || uri == null)
245 Bitmap thumbnail = null;
246 MediaMetadataRetriever retriever =
new MediaMetadataRetriever();
249 retriever.setDataSource(context, uri);
251 thumbnail = retriever.getFrameAtTime(0, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
253 if (thumbnail != null) {
254 thumbnail = Bitmap.createScaledBitmap(thumbnail, width, height,
true);
256 }
catch (Throwable e) {
257 NimLog.e(TAG,
"generateVideoThumbnail error:"+e,e);
262 catch (Throwable e) {
263 NimLog.e(TAG,
"MediaMetadataRetriever release error:"+e,e);
271 if(context == null || uri == null)
275 String fileName = null;
276 Cursor cursor = null;
278 String[] projection = {MediaStore.MediaColumns.DISPLAY_NAME};
279 cursor = context.getContentResolver().query(uri, projection, null, null, null);
280 if (cursor != null && cursor.moveToFirst()) {
281 int index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME);
282 fileName = cursor.getString(index);
285 catch (Throwable e) {
286 NimLog.e(TAG,
"getFileNameFromUri error:"+e,e);
289 if (cursor != null) {
static boolean isFileUri(Uri uri)
判断URI是否为文件类型URI
static Bitmap generateVideoThumbnail(Context context, Uri uri, int width, int height)
从视频文件的URI生成缩略图
static long getFileSizeFromUri(Context context, Uri uri)
从URI中获取文件的实际大小
static String getFileNameFromUri(Context context, Uri uri)
static boolean isFileOrContentUri(String uriString)
判断URI是否为File或者Content类型URI File类型URI表示私有文件 Content类型URI表示共享文件,如图片,音频...
static boolean isFileUri(String uriString)
判断URI是否为文件类型URI
static Uri string2Uri(String uriString)
static boolean isContentUri(Uri uri)
判断URI是否为Content类型URI
static String getFileExtensionFromUri(Context context, Uri uri)
从URI中获取文件的扩展名,如果没有则返回空字符串
static boolean isFileOrContentUri(Uri uri)
判断URI是否为File或者Content类型URI File类型URI表示私有文件 Content类型URI表示共享文件,如图片,音频...
static boolean isContentUri(String uriString)
判断URI是否为Content类型URI
static boolean isFileExists(Context context, Uri uri)
判断文件是否存在