[英]get real path from URI of type data.getData() file:///storage/emulated/0/LenovoReaper/did
[英]Get real path by Uri for any type of file
这是我想在我的应用程序中执行的操作:
因此,我想获取文件并存储的真实路径。 仅允许用户从内部/外部存储中获取文件,因此,按照我的假设,真实路径将始终存在。 我一直在寻找解决方案,几乎每个人都只问有关图像/音频/视频的问题,并在他们的解决方案中使用MediaStore(而到目前为止,我还不知道这是什么,而我不使用它也可以得到Uri)。 如果可能的话,我想要一个适用于任何类型文件的解决方案。
这是我获取Uri的方法:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addFlags(FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("*/*");
startActivityForResult(Intent.createChooser(intent, "Choose File"), 1);
并且我将接收到的信息存储在onActivityResult
方法中。
所以我得到了Uri并将其存储为一个字符串,例如,类似于content://com.android.providers.downloads.documents/document/4588
。 如何将其转换为真实的文件路径?
从我的一个项目共享我的Fileutils文件
public class FileUtils {
public static String getFilePath(Context context, Uri uri) throws URISyntaxException {
String selection = null;
String[] selectionArgs = null;
// Uri is different in versions after KITKAT (Android 4.4), we need to
if (Build.VERSION.SDK_INT >= 19 && DocumentsContract.isDocumentUri(context
.getApplicationContext(), uri)) {
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
return Environment.getExternalStorageDirectory() + "/" + split[1];
} else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
uri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
} else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("image".equals(type)) {
uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
selection = "_id=?";
selectionArgs = new String[]{
split[1]
};
} else if (isGoogleDriveFile(uri)) {
String mimeType = context.getContentResolver().getType(uri);
Cursor returnCursor =
context.getContentResolver().query(uri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
returnCursor.moveToFirst();
String fileName = returnCursor.getString(nameIndex);
Log.d("name", returnCursor.getString(nameIndex));
Log.d("size", Long.toString(returnCursor.getLong(sizeIndex)));
try {
InputStream inputStream = context.getContentResolver().openInputStream(uri);
File root = new File(Environment.getExternalStorageDirectory(), "WittyParrot");
root.mkdirs();
File file = new File(root, fileName);
copyStreamToFile(file, inputStream);
return Uri.fromFile(file).getPath();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
returnCursor.close();
}
}
}
if ("content".equalsIgnoreCase(uri.getScheme())) {
String[] projection = {
MediaStore.Images.Media.DATA
};
Cursor cursor = null;
try {
cursor = context.getContentResolver()
.query(uri, projection, selection, selectionArgs, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
if (cursor.moveToFirst()) {
return cursor.getString(column_index);
}
} catch (Exception e) {
}
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
public static boolean isGoogleDriveFile(Uri uri) {
return "com.google.android.apps.docs.storage".equals(uri.getAuthority());
}
public static void copyStreamToFile(File file, InputStream input) {
try {
OutputStream output = new FileOutputStream(file);
try {
byte[] buffer = new byte[4 * 1024]; // or other buffer size
int read;
while ((read = input.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
output.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.