Android 从uri获取文件名
如何从Android 从uri获取文件名,android,Android,如何从OnActivityResult中返回的uri中获取文件名, 我试着使用这段代码 Uri=data.getData(); 字符串fileName=uri.getLastPathSegment() 但它只返回如下内容图像:3565。拾取的文件不仅是图像类型,还可以是视频或文档文件等。。。。我意识到从kitkat返回的uri也不同于以前的版本,我会对一种同样适用于pre kitkat的方法感兴趣。这是我用来从uri获取信息的代码: public static class FileMetaDat
OnActivityResult
中返回的uri
中获取文件名,
我试着使用这段代码
Uri=data.getData();
字符串fileName=uri.getLastPathSegment()代码>
但它只返回如下内容图像:3565
。拾取的文件不仅是图像类型,还可以是视频或文档文件等。。。。我意识到从kitkat返回的uri也不同于以前的版本,我会对一种同样适用于pre kitkat的方法感兴趣。这是我用来从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;
}
}
对于kotlin,只需使用文件
类中的名称
属性:
val fileName = File(uri.path).name
也许这太琐碎了,但在我的例子中,它起了作用:
DocumentFile.fromSingleUri(上下文,uri).getName()代码>
(简化,没有空指针检查)。与其他元数据类似。根据Android文档
/*
* 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()
...
}
/*
*从传入的意图获取文件的内容URI,
*然后查询服务器应用程序以获取文件的显示名称
*和大小。
*/
returnIntent.data?.let{returnUri->
查询(返回URI,null,null,null,null)
}?使用{游标->
/*
*获取游标中数据的列索引,
*移动到光标的第一行,获取数据,
*并展示它。
*/
val nameIndex=cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
val sizeIndex=cursor.getColumnIndex(OpenableColumns.SIZE)
cursor.moveToFirst()
findViewById(R.id.filename\u text).text=cursor.getString(nameIndex)
findviewbyd(R.id.filesize\u text).text=cursor.getLong(sizeIndex.toString())
...
}
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
}
}