所以我们又要对于 DocumentUri 进行适配。
if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
if (DocumentsContract.isDocumentUri(context, uri)) {
if (isExternalStorageDocument(uri)) {
// ExternalStorageProvider
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(“:”);
final String type = split[0];
if (“primary”.equalsIgnoreCase(type)) {
path = Environment.getExternalStorageDirectory() + “/” + split[1];
return path;
}
} else if (isDownloadsDocument(uri)) {
// DownloadsProvider
final String id = DocumentsContract.getDocumentId(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 = 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]};
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 = 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);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
private static boolean isExternalStorageDocument(Uri uri) {
return “com.android.externalstorage.documents”.equals(uri.getAuthority());
}
private static boolean isDownloadsDocument(Uri uri) {
return “com.android.providers.downloads.documents”.equals(uri.getAuthority());
}
private static boolean isMediaDocument(Uri uri) {
return “com.android.providers.media.documents”.equals(uri.getAuthority());
}
好了,上面的代码还是容易看懂的。这下就解决了对于 Android 4.4 及以上的机型适配。顺便把以 file:// 开头的 URI 适配也补上:
if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
path = uri.getPath();
return path;
}
完美了,下面就贴出完整的 FileUtils 代码,拿去用吧:
public final class FileUtils {
public static String getFilePathByUri(Context context, Uri uri) {
String path = null;
// 以 file:// 开头的
if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
path = uri.getPath();
return path;
}
// 以 content:// 开头的,比如 content://media/extenral/images/media/17766
if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()) && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
if (columnIndex > -1) {
path = cursor.getString(columnIndex);
}
}
cursor.close();
}
return path;
}
// 4.4及之后的 是以 content:// 开头的,比如 content://com.android.providers.media.documents/document/image%3A235700
if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (DocumentsContract.isDocumentUri(context, uri)) {
if (isExternalStorageDocument(uri)) {
// ExternalStorageProvider
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(“:”);
final String type = split[0];
if (“primary”.equalsIgnoreCase(type)) {
path = Environment.getExternalStorageDirectory() + “/” + split[1];
return path;
}
} else if (isDownloadsDocument(uri)) {
// DownloadsProvider
final String id = DocumentsContract.getDocumentId(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 = 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]};
path = getDataColumn(context, contentUri, selection, selectionArgs);
return path;
}
}
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
最后
给大家送上我成功跳槽复习中所整理的资料,由于文章篇幅有限,所以只是把题目列出来了
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!
图片转存中…(img-PXTInHbk-1712244047801)]
[外链图片转存中…(img-UornhlH8-1712244047801)]