Android根据Uri获取文件的绝对路径
程序员文章站
2022-05-10 08:45:53
...
public class Uri2PathUtil {
//复杂版处理 (适配多种API)
public static String getRealPathFromUri(Context context, Uri uri) {
int sdkVersion = Build.VERSION.SDK_INT;
if (sdkVersion < 11) return getRealPathFromUri_BelowApi11(context, uri);
if (sdkVersion < 19) return getRealPathFromUri_Api11To18(context, uri);
else return getRealPathFromUri_AboveApi19(context, uri);
}
/**
* 适配api19以上,根据uri获取图片的绝对路径
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
private static String getRealPathFromUri_AboveApi19(Context context, Uri uri) {
if (DocumentsContract.isDocumentUri(context, uri)) {
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];
}
} 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);
} else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri;
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;
} else {
contentUri = MediaStore.Files.getContentUri("external");
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{split[1]};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
return getDataColumn(context, uri, null, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
/**
* 适配api11-api18,根据uri获取图片的绝对路径
*/
private static String getRealPathFromUri_Api11To18(Context context, Uri uri) {
String filePath = null;
String[] projection = {MediaStore.Images.Media.DATA};
//这个有两个包不知道是哪个。。。。不过这个复杂版一般用不到
CursorLoader loader = new CursorLoader(context, uri, projection, null, null, null);
Cursor cursor = loader.loadInBackground();
if (cursor != null) {
cursor.moveToFirst();
filePath = cursor.getString(cursor.getColumnIndex(projection[0]));
cursor.close();
}
return filePath;
}
/**
* 适配api11以下(不包括api11),根据uri获取图片的绝对路径
*/
private static String getRealPathFromUri_BelowApi11(Context context, Uri uri) {
String filePath = null;
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
filePath = cursor.getString(cursor.getColumnIndex(projection[0]));
cursor.close();
}
return filePath;
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
String column = MediaStore.MediaColumns.DATA;
String[] projection = {column};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} catch (Exception e) {
e.printStackTrace();
} 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());
}
}
上一篇: 大数据的大麻烦即将出现
下一篇: 不娶美女三个理由
推荐阅读
-
javascript - php 有什么函数是可以根据文件名称,来获取这个文件的全路径的吗?或者js 怎么获取文件的全路径?
-
适配android7.0获取文件的Uri的方法
-
Java/Android 获取网络重定向文件的真实URL的示例代码
-
适配android7.0获取文件的Uri的方法
-
Android编程实现根据经纬度查询地址并对获取的json数据进行解析的方法
-
Android编程获取sdcard音乐文件的方法
-
Android编程实现根据经纬度查询地址并对获取的json数据进行解析的方法
-
Android获取本机各种类型文件的方法
-
Android编程获取sdcard音乐文件的方法
-
android 获取文件的扩展名和去掉文件扩展名的小例子