android把U盘的uri转为路径
时间: 2023-10-25 11:06:38 浏览: 163
在 Android 11 中,访问外部存储需要申请 `MANAGE_EXTERNAL_STORAGE` 权限。如果没有申请该权限,会导致访问外部存储时出现错误。
可以参考以下代码,在 `AndroidManifest.xml` 文件中申请该权限:
```xml
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
```
同时,在运行时也需要动态申请该权限。
以下是将 U 盘的 Uri 转换为路径的示例代码:
```java
public class FileUtils {
public static String getUsbDiskPath(Context context) {
String usbDiskPath = null;
File[] dirs = ContextCompat.getExternalFilesDirs(context, null);
for (File dir : dirs) {
if (dir != null && dir.exists() && dir.isDirectory() && dir.canWrite()) {
String dirPath = dir.getAbsolutePath();
if (dirPath.startsWith("/storage/")) {
// 内置存储
continue;
} else {
// 外置存储
usbDiskPath = dirPath;
break;
}
}
}
return usbDiskPath;
}
public static String getRealPathFromUri(Context context, Uri uri) {
String realPath = null;
if (DocumentsContract.isDocumentUri(context, uri)) {
if (isExternalStorageDocument(uri)) {
// 外部存储
String docId = DocumentsContract.getDocumentId(uri);
String[] split = docId.split(":");
String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
realPath = Environment.getExternalStorageDirectory() + "/" + split[1];
} else {
String usbDiskPath = getUsbDiskPath(context);
if (usbDiskPath != null) {
realPath = usbDiskPath + "/" + split[1];
}
}
} else if (isDownloadsDocument(uri)) {
// 下载目录
String id = DocumentsContract.getDocumentId(uri);
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
realPath = getDataColumn(context, contentUri, null, null);
} else if (isMediaDocument(uri)) {
// 媒体库
String docId = DocumentsContract.getDocumentId(uri);
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 = "_id=?";
String[] selectionArgs = new String[]{split[1]};
realPath = getDataColumn(context, contentUri, selection, selectionArgs);
}
} else if (ContentResolver.SCHEME_FILE.equalsIgnoreCase(uri.getScheme())) {
// 直接从文件中获取
realPath = uri.getPath();
}
return realPath;
}
private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
String column = "_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);
}
} catch (Exception e) {
e.printStackTrace();
} 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());
}
}
```
该工具类中的 `getRealPathFromUri` 方法可以将 U 盘的 Uri 转换为路径。具体实现是根据 Uri 的不同类型,采用不同的方式获取路径。
阅读全文