Google Android开发者文档系列-创建有内容分享特性的应用之获取文件信息

来源:互联网 发布:c盘windows文件夹清理 编辑:程序博客网 时间:2024/05/17 23:55

Retrieving File Information(获取文件信息)

该系列文章是我在学习Google开发者文档时结合谷歌翻译和自身理解编写的,希望对学习Android开发的朋友带来些便利,由于个人翻译水平有限,所以内容包含原文和译文,希望浏览者结合理解,以免步入我可能错译的误区。在此感谢http://android.xsoftlab.net/提供的镜像,希望转载者注明出处http://blog.csdn.net/u014031072/article/details/51597286方便查看最新博客

Before a client app tries to work with a file for which it has a content URI, the app can request information about the file from the server app, including the file’s data type and file size. The data type helps the client app to determine if it can handle the file, and the file size helps the client app set up buffering and caching for the file.
在一个客户端app尝试着操作包含一个content URI的文件前,客户端app可以向服务端app请求文件的信息,包括文件的数据类型和文件大小。文件类型帮助客户端app决定它是否可以处理该文件,文件大小帮助客户端app为该文件创建缓冲和缓存。

This lesson demonstrates how to query the server app’s FileProvider to retrieve a file’s MIME type and size.
这节课示范了如何查询服务端app的FileProvider来获得文件的MIME类型和大小。

Retrieve a File’s MIME Type(获取一个文件的MIME类型)

A file’s data type indicates to the client app how it should handle the file’s contents. To get the data type of a shared file given its content URI, the client app calls ContentResolver.getType(). This method returns the file’s MIME type. By default, a FileProvider determines the file’s MIME type from its filename extension.
文件的数据类型指示了客户端app该如何操作文件的内容。为了得到获取的content URI对应的共享文件的数据类型,客户端app需要调用ContentResolver.getType()方法。这个方法返回该文件的MIME类型。默认的,FileProvider通过文件的扩展名来决定文件的MIME类型。

The following code snippet demonstrates how a client app retrieves the MIME type of a file once the server app has returned the content URI to the client:
下面的代码片示范了服务端app返回给客户端appcontent URI后,客户端app如何获得文件的MIME类型:

.../* * Get the file's content URI from the incoming Intent, then * get the file's MIME type */ //从intent中获取文件的content URI,然后获取文件的MIME类型Uri returnUri = returnIntent.getData();String mimeType = getContentResolver().getType(returnUri);...

Retrieve a File’s Name and Size(获取文件的名字和大小)

The FileProvider class has a default implementation of the query() method that returns the name and size of the file associated with a content URI in a Cursor. The default implementation returns two columns:
FileProvider 有一个默认query()方法的实现,它会返回一个包含与content URI关联文件的名称和大小信息的Cursor对象。默认的实现返回两列:

DISPLAY_NAME

The file’s name, as a String. This value is the same as the value returned by File.getName().
文件的字符串名称。值与File.getName()方法返回值相同。

SIZE

The size of the file in bytes, as a long This value is the same as the value returned by File.length()
文件的(字节)大小,一个和File.length()方法返回值相同的long型数值。

The client app can get both the DISPLAY_NAME and SIZE for a file by setting all of the arguments of query() to null except for the content URI. For example, this code snippet retrieves a file’s DISPLAY_NAME and SIZE and displays each one in separate TextView:
通过将query()方法的除content URI之外的参数都设置为null.客户端app 能够同时获得文件的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.     */     //获取文件的content URI然后查询获取服务端文件的名称和大小信息    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.     */     //获取cursor中的数据的列索引,将cursor指向第一行,获取数据并显示    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
原创粉丝点击