Android如何通过URI获取⽂件路径⽰例代码
前⾔
最近在⼯作的过程中,遇到不同 Android 版本下 URI 采⽤不同⽅式来获取⽂件路径的问题。
因为需求的原因,要求拍照上传或者从相册中选择图⽚上传,⽽且图⽚是需要经过压缩的,⼤⼩不能超过2M。
很快,拍照的这部分就搞定了。那么相册中选择图⽚的也是⼀样的道理,应该也是轻松解决了。
⾄于选择图⽚的代码,如下所⽰:
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, FILE_CHOOSER_RESULT_CODE);
之后就是在onActivityResult(int requestCode, int resultCode, Intent data) 中获取到 URI 。
最关键的来了,如果通过 URI 来获取⽂件呢?
⽐如,现在 URI 为 content://media/extenral/images/media/17766 ,⽽我们需要得到对应的⽂件路径。
if (ContentResolver.SCHEME_CONTENT.Scheme())) {
Cursor cursor = ContentResolver().query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null);
if (cursor != null) {
if (veToFirst()) {
int columnIndex = ColumnIndexOrThrow(MediaStore.Images.Media.DATA);
if (columnIndex > -1) {
path = String(columnIndex);
}
}
cursor.close();
}
return path;
}
原以为万事⼤吉,但是在 Android 4.4 及以上的⼿机上⼀试,发现根本不⾏。因为在 Android 4.4 及以上的⼿机上,获取到的URI 变成了 content://com.dia.documents/document/image%3A235700 ,和之前我们预期的不是同⼀种类型。
这是因为在 Android 4.4 及以上的机型,使⽤了 DocumentUri 来代表获取到⽂件的 URI 。
所以我们⼜要对于 DocumentUri 进⾏适配。
if (ContentResolver.SCHEME_CONTENT.Scheme())) {
if (DocumentsContract.isDocumentUri(context, uri)) {
if (isExternalStorageDocument(uri)) {
// ExternalStorageProvider
final String docId = DocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
path = ExternalStorageDirectory() + "/" + split[1];
return path;
}
} else if (isDownloadsDocument(uri)) {
// DownloadsProvider
final String id = DocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
Long.valueOf(id));
path = getDataColumn(context, contentUri, null, null);
return path;
} else if (isMediaDocument(uri)) {
// MediaProvider
final String docId = DocumentId(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]};
path = getDataColumn(context, contentUri, selection, selectionArgs);
return path;
}
}
}
private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {column};
try {
cursor = ContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && veToFirst()) {
final int column_index = ColumnIndexOrThrow(column);
String(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
private static boolean isExternalStorageDocument(Uri uri) {
return "alstorage.documents".Authority());
}
private static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".Authority());
}
private static boolean isMediaDocument(Uri uri) { return "com.dia.documents".Authority());
}
好了,上⾯的代码还是容易看懂的。这下就解决了对于 Android 4.4 及以上的机型适配。顺便把以 file:// 开头的 URI 适配也补上:
if (ContentResolver.SCHEME_FILE.Scheme())) {
path = Path();
return path;
}
完美了,下⾯就贴出完整的 FileUtils 代码,拿去⽤吧:
public final class FileUtils {
public static String getFilePathByUri(Context context, Uri uri) {
String path = null;
// 以 file:// 开头的
if (ContentResolver.SCHEME_FILE.Scheme())) {
video安卓下载path = Path();
return path;
}
// 以 content:// 开头的,⽐如 content://media/extenral/images/media/17766
if (ContentResolver.SCHEME_CONTENT.Scheme()) && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
Cursor cursor = ContentResolver().query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null);
if (cursor != null) {
if (veToFirst()) {
int columnIndex = ColumnIndexOrThrow(MediaStore.Images.Media.DATA);
if (columnIndex > -1) {
path = String(columnIndex);
}
}
cursor.close();
}
return path;
}
// 4.4及之后的是以 content:// 开头的,⽐如 content://com.dia.documents/document/image%3A235700
if (ContentResolver.SCHEME_CONTENT.Scheme()) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (DocumentsContract.isDocumentUri(context, uri)) {
if (isExternalStorageDocument(uri)) {
// ExternalStorageProvider
final String docId = DocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
path = ExternalStorageDirectory() + "/" + split[1];
return path;
}
} else if (isDownloadsDocument(uri)) {
// DownloadsProvider
final String id = DocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
Long.valueOf(id));
path = getDataColumn(context, contentUri, null, null);
return path;
} else if (isMediaDocument(uri)) {
/
/ MediaProvider
final String docId = DocumentId(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]};
path = getDataColumn(context, contentUri, selection, selectionArgs);
return path;
}
}
}
return null;
}
private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {column};
try {
cursor = ContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && veToFirst()) {
final int column_index = ColumnIndexOrThrow(column);
String(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
private static boolean isExternalStorageDocument(Uri uri) {
return "alstorage.documents".Authority());
}
private static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".Authority());
}
private static boolean isMediaDocument(Uri uri) { return "com.dia.documents".Authority());
}
}
讲完了,讲完了。
总结
以上就是这篇⽂章的全部内容了,希望本⽂的内容对⼤家的学习或者⼯作具有⼀定的参考学习价值,如果有疑问⼤家可以留⾔交流,谢谢⼤家对的⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论