《Training:Retrieving File Information》

注:本人非专业翻译人员,编写此博客只是为了自学使用,如有疑问,请参考官方文档。



在客户端应用程序尝试使用具有内容URI的文件之前,应用程序可以从服务器应用程序请求有关文件的信息,包括文件的数据类型和文件大小。 数据类型帮助客户端应用程序确定是否可以处理该文件,并且文件大小有助于客户端应用程序为文件设置缓冲和缓存。


本课程演示如何查询服务器应用程序的FileProvider以检索文件的MIME类型和大小。



一、检索文件的MIME类型

文件的数据类型向客户端应用程序指示如何处理文件的内容。 要获取其内容URI的共享文件的数据类型,客户端应用程序将调用ContentResolver.getType()。 此方法返回文件的MIME类型。 默认情况下,FileProvider根据文件扩展名确定文件的MIME类型。


以下代码段演示了客户端应用程序如何在服务器应用程序将内容URI返回给客户端后检索文件的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()方法的默认实现,该方法返回与Cursor中的内容URI相关联的文件的名称和大小。 默认实现返回两列:

DISPLAY_NAME:该文件的名称,作为一个字符串。 该值与File.getName()返回的值相同。

SIZE:文件的大小以字节为单位,长度与File.length()返回的值相同。


除了内容URI之外,客户端应用程序可以通过将query()的所有参数设置为null,来获取文件的DISPLAY_NAME和SIZE。例如,此代码段检索文件的DISPLAY_NAME和SIZE,并将其显示在单独的TextView中:

...
    /*
     * 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)));
    ...


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值