如何从uri
中返回的OnActivityResult
获取文件名,
我尝试使用这段代码
Uri uri = data.getData();
String fileName = uri.getLastPathSegment();
但它只返回类似images:3565
的内容。挑选的文件不仅是图像类型,它可以是视频或文档文件等....我意识到从kitkat返回的uri与以前的版本不同,我会感兴趣的方法是适用于pre kitkat。
答案 0 :(得分:4)
这是我用来从Uri获取信息的代码:
public static class FileMetaData
{
public String displayName;
public long size;
public String mimeType;
public String path;
@Override
public String toString()
{
return "name : " + displayName + " ; size : " + size + " ; path : " + path + " ; mime : " + mimeType;
}
}
public static FileMetaData getFileMetaData(Context context, Uri uri)
{
FileMetaData fileMetaData = new FileMetaData();
if ("file".equalsIgnoreCase(uri.getScheme()))
{
File file = new File(uri.getPath());
fileMetaData.displayName = file.getName();
fileMetaData.size = file.length();
fileMetaData.path = file.getPath();
return fileMetaData;
}
else
{
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(uri, null, null, null, null);
fileMetaData.mimeType = contentResolver.getType(uri);
try
{
if (cursor != null && cursor.moveToFirst())
{
int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
fileMetaData.displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
if (!cursor.isNull(sizeIndex))
fileMetaData.size = cursor.getLong(sizeIndex);
else
fileMetaData.size = -1;
try
{
fileMetaData.path = cursor.getString(cursor.getColumnIndexOrThrow("_data"));
}
catch (Exception e)
{
// DO NOTHING, _data does not exist
}
return fileMetaData;
}
}
catch (Exception e)
{
Log.e(Log.TAG_CODE, e);
}
finally
{
if (cursor != null)
cursor.close();
}
return null;
}
}
答案 1 :(得分:1)
也许这太琐碎了,但就我而言,它奏效了:
DocumentFile.fromSingleUri(context, uri).getName();
(简化,没有空指针检查)。与其他元数据类似。
答案 2 :(得分:0)
对于Kotlin,只需使用name
类中的File
属性:
val fileName = File(uri.path).name
答案 3 :(得分:0)
根据安卓文档
/*
* Get the file's content URI from the incoming Intent,
* then query the server app to get the file's display name
* and size.
*/
returnIntent.data?.let { returnUri ->
contentResolver.query(returnUri, null, null, null, null)
}?.use { cursor ->
/*
* Get the column indexes of the data in the Cursor,
* move to the first row in the Cursor, get the data,
* and display it.
*/
val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
val sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE)
cursor.moveToFirst()
findViewById<TextView>(R.id.filename_text).text = cursor.getString(nameIndex)
findViewById<TextView>(R.id.filesize_text).text = cursor.getLong(sizeIndex).toString()
...
}
https://developer.android.com/training/secure-file-sharing/retrieve-info
答案 4 :(得分:0)
fun Uri.getFileNameWithExtension(context: Context): String? {
val name = this.path?.let { path -> File(path).name }.orEmpty()
val extension = MimeTypeMap.getSingleton()
.getExtensionFromMimeType(getMimeType(context)).orEmpty()
return if (name.isNotEmpty() && extension.isNotEmpty()) "$name.$extension" else null
}
fun Uri.getMimeType(context: Context): String? {
return when (scheme) {
ContentResolver.SCHEME_CONTENT -> context.contentResolver.getType(this)
ContentResolver.SCHEME_FILE -> MimeTypeMap.getSingleton().getMimeTypeFromExtension(
MimeTypeMap.getFileExtensionFromUrl(toString()).toLowerCase(Locale.US)
)
else -> null
}
}
答案 5 :(得分:0)
我认为从 URI 检索信息的最直接和最简单的方法是使用 DocumentFile。只需使用上下文和您的 URI 创建一个新的 DocumentFile。
DocumentFile file = DocumentFile.fromSingleUri(context, uri);
然后您可以从中检索各种信息。
String fileName = file.getName();
long fileSize = file.length();
String mimeType = file.getType(); //get the mime type
注意 file.getName() 将返回文件名带扩展名(例如 video.mp4)