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))); ...
- Google Android开发者文档系列-创建有内容分享特性的应用之获取文件信息
- Google Android开发者文档系列-创建有内容分享特性的应用之设置共享文件
- Google Android开发者文档系列-创建有内容分享特性的应用之共享文件
- Google Android开发者文档系列-创建有内容分享特性的应用之请求共享文件
- Google Android开发者文档系列-创建有内容分享特性的应用之文件共享(序言)
- Google Android开发者文档系列-创建有内容分享特性的应用之接收其它应用程序发送的简单数据
- Google Android开发者文档系列-创建有内容分享特性的应用之添加一个简单的共享action
- Google Android开发者文档系列-创建有内容分享特性的应用之发送简单数据到其它应用程序
- Android官方开发文档Training系列课程中文版:分享文件之获取文件信息
- Google Android开发者文档系列-与其他应用程序交互之获取Activity返回的结果
- Google Android开发者文档系列-与其他应用程序交互之允许其他应用启动你的Activity
- Google Android开发者文档系列-数据存储之通过SQL数据库保存数据
- Google Android开发者文档系列-与其他应用程序交互之使用户跳转到其它应用程序
- Google Android开发者文档系列-开发企业App
- Android官方开发文档Training系列课程中文版:分享文件之分享一个文件
- 创建可以分享内容的应用
- Google Training 建立分享内容的APP ------ 分享文件
- Android官方开发文档Training系列课程中文版:分享文件之请求一个共享文件
- HDU 5523 Game
- Cursor排序
- 将CentOS目录映射为Windows驱动器
- Java 并发包中的读写锁及其实现分析
- Java进阶之自动拆箱与自动装箱
- Google Android开发者文档系列-创建有内容分享特性的应用之获取文件信息
- 较之java代码在jsp中
- 【优秀博客推荐】
- 打开相册并显示图片
- 使用CSS样式表
- ctags简易用法
- 安卓判断某APP是否在运行
- 5.6 混合密度网络
- 开个包随便说说