您的位置:首页 > 移动开发

和其他App分享文件(4)获取文件信息

2015-01-06 14:57 375 查看
获取文件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
类有默认实现
, java.lang.String, java.lang.String[], java.lang.String)]query()
方法,这个方法返回文件名和大小在相关联的content URI的Cursor中。

默认实现返回两列:

DISPLAY_NAME

一个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)));
...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐