获取文件MIME类型
...
/*
* Get the file's content URI from the incoming Intent, then
* get the file's MIME type
*/
Uri returnUri = returnIntent.getData();
String mimeType = getContentResolver().getType(returnUri);
...
获取文件名称和大小
FileProvider 类有默认实现query() 方法,这个方法返回文件名和大小在相关联的content URI的Cursor中。
默认实现返回两列:
一个String的文件名,等同于File.getName()的返回值。
SIZE:
文件字节大小,是一个long值等同于File.length()。
例子如下:
...
/*
* Get the file's content URI from the incoming Intent,
* then query the server app to get the file's display name
* and size.
*/
Uri returnUri = returnIntent.getData();
Cursor returnCursor =
getContentResolver().query(returnUri, null, null, null, null);
/*
* Get the column indexes of the data in the Cursor,
* move to the first row in the Cursor, get the data,
* and display it.
*/
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
returnCursor.moveToFirst();
TextView nameView = (TextView) findViewById(R.id.filename_text);
TextView sizeView = (TextView) findViewById(R.id.filesize_text);
nameView.setText(returnCursor.getString(nameIndex));
sizeView.setText(Long.toString(returnCursor.getLong(sizeIndex)));
...
本文将指导您如何从传入的Intent获取文件的内容URI,并从中获取文件的MIME类型、名称和大小。通过使用FileProvider类的query()方法,您可以轻松地在相关联的content URI的Cursor中检索到文件名和大小。示例代码展示了如何利用这些信息,为文件命名和显示大小提供数据。

被折叠的 条评论
为什么被折叠?



