【Android Developers Training】 39. 获取文件

系统 1609 0

注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好。

原文链接: http://developer.android.com/training/secure-file-sharing/retrieve-info.html


当一个客户端应用尝试对一个有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

是文件的文件名,一个 String 。这个值和 File.getName() 所返回的值是一样的。

SIZE

文件的大小,字节为单位,一个“long”型。这个值和 File.length() 所返回的值是一样的。

客户端应用可以通过将 query() 的参数都设置为“ null ”,值保留URI这一参数,来同时获取文件的名字和尺寸。例如,下面的代码获取一个文件的名称和大小,然后在两个 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)));

    ...
      
    

【Android Developers Training】 39. 获取文件信息


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论