专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Go

Google Android开发者文档系列-创设有内容分享特性的应用之共享文件

发布时间:2011-06-29 17:59:07 文章来源:www.iduyao.cn 采编人员:星星草
Google Android开发者文档系列-创建有内容分享特性的应用之共享文件

Sharing a File(共享文件)

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

Once you have set up your app to share files using content URIs, you can respond to other apps’ requests for those files. One way to respond to these requests is to provide a file selection interface from the server app that other applications can invoke. This approach allows a client application to let users select a file from the server app and then receive the selected file’s content URI.
一旦你已经在你的app中通过content URI设置好文件共享后,你就能回应其他app对应的文件请求了。响应这些请求的一种方式是从服务app提供一个其他app可以请求文件的选择界面。这种方法使客户端app允许用户选择服务器上一项文件,然后接收所选文件的content URI。

This lesson shows you how to create a file selection Activity in your app that responds to requests for files.
这节课向你展示了如何在你的app中创建一个文件选择的activity来响应文件请求。

Receive File Requests(获取文件请求)

To receive requests for files from client apps and respond with a content URI, your app should provide a file selection Activity. Client apps start this Activity by calling startActivityForResult() with an Intent containing the action ACTION_PICK. When the client app calls startActivityForResult(), your app can return a result to the client app, in the form of a content URI for the file the user selected.
从客户端应用程序接收文件的请求,响应并返回一个content URI,您的app应提供一个文件选择activity。客户端应用程序通过调用startActivityForResult()方法并传入一个包含ACTION_PICK的action的Intent来启动该请求活动。当客户端应用程序调用startActivityForResult()方法时,您的应用程序可以以用户选择的文件内容URI的形式,将结果返回给客户端应用程序。

To learn how to implement a request for a file in a client app, see the lesson Requesting a Shared File.
学习如何在客户端app中实现一个文件请求,请参考Requesting a Shared File课程。

Create a File Selection Activity(创建一个文件选择的activity)

To set up the file selection Activity, start by specifying the Activity in your manifest, along with an intent filter that matches the action ACTION_PICK and the categories CATEGORY_DEFAULT and CATEGORY_OPENABLE. Also add MIME type filters for the files your app serves to other apps. The following snippet shows you how to specify the new Activity and intent filter:
要创建一个文件选择的activity,先在你的清单文件中定义一个activity,包含一个匹配ACTION_PICK的action的intent filter,它的categories值为CATEGORY_DEFAULT和CATEGORY_OPENABLE。同时添加一个你的app为其他app服务的MIME类型过滤器。下面的代码片展示了如何定义新的activity和它的intent filter:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    ...
        <application>
        ...
            <activity
                android:name=".FileSelectActivity"
                android:label="@"File Selector" >
                <intent-filter>
                    <action
                        android:name="android.intent.action.PICK"/>
                    <category
                        android:name="android.intent.category.DEFAULT"/>
                    <category
                        android:name="android.intent.category.OPENABLE"/>
                    <data android:mimeType="text/plain"/>
                    <data android:mimeType="image/*"/>
                </intent-filter>
            </activity>

Define the file selection Activity in code(在代码中定义文件选择的activity)

Next, define an Activity subclass that displays the files available from your app’s files/images/ directory in internal storage and allows the user to pick the desired file. The following snippet demonstrates how to define this Activity and respond to the user’s selection:
下一步,定义一个activity子类来显示你的app内部存储中files/images/路径下可用的文件并允许用户选择期望的文件。下面的代码片示范了如何定义这个activity并响应用户的选择:

public class MainActivity extends Activity {
    // The path to the root of this app's internal storage
    //app的内部存储根路径 
    private File mPrivateRootDir;
    // The path to the "images" subdirectory
    //images子路径
    private File mImagesDir;
    // Array of files in the images subdirectory
    //images子路径下的文件列表
    File[] mImageFiles;
    // Array of filenames corresponding to mImageFiles
    // iamges文件列表中对应的文件名列表
    String[] mImageFilenames;
    // Initialize the Activity
    //初始化activity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        // Set up an Intent to send back to apps that request a file
        //定义一个intent来送回给发出文件请求的app
        mResultIntent =
                new Intent("com.example.myapp.ACTION_RETURN_FILE");
        // Get the files/ subdirectory of internal storage
        //获取内存中files/路径的子路径
        mPrivateRootDir = getFilesDir();
        // Get the files/images subdirectory;
        //获取files/images路径的子路径
        mImagesDir = new File(mPrivateRootDir, "images");
        // Get the files in the images subdirectory
        //获取images子路径下的文件列表
        mImageFiles = mImagesDir.listFiles();
        // Set the Activity's result to null to begin with
        //设置activity的返回结果为null   
        setResult(Activity.RESULT_CANCELED, null);
        /*
         * Display the file names in the ListView mFileListView.
         * Back the ListView with the array mImageFilenames, which
         * you can create by iterating through mImageFiles and
         * calling File.getAbsolutePath() for each File
         */

         ...
    }
    ...
}

Respond to a File Selection(响应文件选择界面)

Once a user selects a shared file, your application must determine what file was selected and then generate a content URI for the file. Since the Activity displays the list of available files in a ListView, when the user clicks a file name the system calls the method onItemClick(), in which you can get the selected file.
一旦用户选择了一项共享文件,你的应用必须决定哪个文件被选择了然后产生该文件的content URI。因为activity在listview中显示可用的文件列表,当用户点击文件名称时,系统调用listview的onItemClick()方法,在这个方法中你可以获取到被选择的文件。

In onItemClick(), get a File object for the file name of the selected file and pass it as an argument to getUriForFile(), along with the authority that you specified in the <?(去掉问号)provider> element for the FileProvider. The resulting content URI contains the authority, a path segment corresponding to the file’s directory (as specified in the XML meta-data), and the name of the file including its extension. How FileProvider maps directories to path segments based on XML meta-data is described in the section Specify Sharable Directories.
在onItemClick()中,获取一个对应文件名称的文件对象,和在FileProvider相关的<?(去掉问号)provider>要素中定义好的权限一起,作为参数传递给getUriForFile()方法。得到的结果content URI包含权限、对应于该文件的目录(如在XML中指定)的路径段和文件包含其扩展名的文件名称。FileProvider 如何映射路径片段的文件路径,基于Specify Sharable Directories章节中关于xml meta-data的描述

The following snippet shows you how to detect the selected file and get a content URI for it:
下面的代码片展示了如何检测被选中的文件并获得一个对应其的content URI:

 protected void onCreate(Bundle savedInstanceState) {
        ...
        // Define a listener that responds to clicks on a file in the ListView
        //定义一个监听器来响应listview中的点击事件
        mFileListView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
            @Override
            /*
             * When a filename in the ListView is clicked, get its
             * content URI and send it to the requesting app
             */
             //当listview中的一个文件名被点击后,获取它的content URI并把它发送给请求app
            public void onItemClick(AdapterView<?> adapterView,
                    View view,
                    int position,
                    long rowId) {
                /*
                 * Get a File for the selected file name.
                 * Assume that the file names are in the
                 * mImageFilename array.
                 */
                 //获取一个被选中文件名对应的文件,假设文件名在mImageFilename数组中
                File requestFile = new File(mImageFilename[position]);
                /*
                 * Most file-related method calls need to be in
                 * try-catch blocks.
                 */
                 //大部分文件相关的方法调用时需要在try-catch块中进行
                // Use the FileProvider to get a content URI
                //使用FileProvider来获取一个content URI
                try {
                    fileUri = FileProvider.getUriForFile(
                            MainActivity.this,
                            "com.example.myapp.fileprovider",
                            requestFile);
                } catch (IllegalArgumentException e) {
                    Log.e("File Selector",
                          "The selected file can't be shared: " +
                          clickedFilename);
                }
                ...
            }
        });
        ...
    }

Remember that you can only generate content URIs for files that reside in a directory you’ve specified in the meta-data file that contains the <?(去掉问号)paths> element, as described in the section Specify Sharable Directories. If you call getUriForFile() for a File in a path that you haven’t specified, you receive an IllegalArgumentException.
记住,你只能产生在meta-data文件中包括的<?(去掉问号)paths>要素中定义好的文件路径(在Specify Sharable Directories章节中描述的那样)对应的文件的content URI。如果你为一个没有定义好的文件调用getUriForFile()方法,你将会收到一个IllegalArgumentException异常。

Grant Permissions for the File(为文件授权)

Now that you have a content URI for the file you want to share with another app, you need to allow the client app to access the file. To allow access, grant permissions to the client app by adding the content URI to an Intent and then setting permission flags on the Intent. The permissions you grant are temporary and expire automatically when the receiving app’s task stack is finished.
现在你已经获得了一个你想共享给其他app的文件的content URI,你需要允许客户端app进入文件。为了进入文件,通过添加content URI到一个intent然后设置权限标签给该intent来给客户端app授权。你所授的权限时暂时并当接收app的堆栈任务完成后可以自动取消的。

The following code snippet shows you how to set read permission for the file:
下面的代码片展示了如何设置文件的读取权限:

 protected void onCreate(Bundle savedInstanceState) {
        ...
        // Define a listener that responds to clicks in the ListView
        mFileListView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView,
                    View view,
                    int position,
                    long rowId) {
                ...
                if (fileUri != null) {
                    // Grant temporary read permission to the content URI
                    //给content URI设置暂时读取权限
                    mResultIntent.addFlags(
                        Intent.FLAG_GRANT_READ_URI_PERMISSION);
                }
                ...
             }
             ...
        });
    ...
    }

Caution: Calling setFlags() is the only way to securely grant access to your files using temporary access permissions. Avoid calling Context.grantUriPermission() method for a file’s content URI, since this method grants access that you can only revoke by calling Context.revokeUriPermission().
注意:调用setFlags()方法是使用暂时进入权限授予你的文件进入权限唯一安全的方法。避免为文件的content URI调用Context.grantUriPermission()方法,因为这个方法获取的权限只能通过调用Context.revokeUriPermission()方法来取消权限。

Share the File with the Requesting App(向请求app共享文件)

To share the file with the app that requested it, pass the Intent containing the content URI and permissions to setResult(). When the Activity you have just defined is finished, the system sends the Intent containing the content URI to the client app. The following code snippet shows you how to do this:
要向请求文件的app共享文件,需要传递包含content URI和权限的intent到setResult()方法。当你定义完这些操作所处的activity销毁后,系统发送包含content URI的intent到客户端app。下面的代码片展示了如何去实现:

 protected void onCreate(Bundle savedInstanceState) {
        ...
        // Define a listener that responds to clicks on a file in the ListView
        mFileListView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView,
                    View view,
                    int position,
                    long rowId) {
                ...
                if (fileUri != null) {
                    ...
                    // Put the Uri and MIME type in the result Intent
                    mResultIntent.setDataAndType(
                            fileUri,
                            getContentResolver().getType(fileUri));
                    // Set the result
                    MainActivity.this.setResult(Activity.RESULT_OK,
                            mResultIntent);
                    } else {
                        mResultIntent.setDataAndType(null, "");
                        MainActivity.this.setResult(RESULT_CANCELED,
                                mResultIntent);
                    }
                }
        });

Provide users with an way to return immediately to the client app once they have chosen a file. One way to do this is to provide a checkmark or Done button. Associate a method with the button using the button’s android:onClick attribute. In the method, call finish(). For example:
一旦用户已经选择好了一个文件,就向他们提供一个方法立即返回到客户端app。一个方法是提供一个标志或完成按钮。通过使用按钮的android:onClick属性关联一个方法给按钮。在方法中调用finish()方法。例如:

 public void onDoneClick(View v) {
        // Associate a method with the Done button
        finish();
    }
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

  • ModernUI课程:定义一个Logo

    ModernUI教程:定义一个Logo ModernWindow的标题栏包含了一块区域用来显示自定义的窗体Logo: 这个窗体logo通过ModernWindow.LogoD...

  • Django忘记管理员账号和密码的解决方法

    Django忘记管理员账号和密码的解决办法 看着Django的教程学习搭建网站,结果忘记第一次创建的账号和密码了。结果搭建成功以后,一直...

  • GO语言小结(1)——基本知识

    GO语言总结(1)——基本知识 1、注释(与C++一样)   行注释://  块注释:/*   ...  */ 2、标识符   可以这么说,除了数字开头...

  • golang 惯用的文件读取方式

    golang 常用的文件读取方式 Golang 的文件读取方法很多,刚上手时不知道怎么选择,所以贴在此处便后速查。 一次性读取 小文件推荐一...

  • 查询深圳市通相关信息

    查询深圳通相关信息 用 HTTP.GET 从开放 API 中查询深圳通信息,然后将 JSON 数据存入结构体中,再格式化输出。 注意:获取的并不是实...

  • Go语言设计模式实践:结合(Composite)

    Go语言设计模式实践:组合(Composite) 关于本系列 这个系列首先是关于Go语言实践的。在项目中实际使用Go语言也有段时间了,一个体会就...

  • 列出索引和遍历目录

    列出目录和遍历目录 获取目录列表用 ioutil.ReadDir(),遍历目录用 filepath.Walk(),使用方法请参考文章示例。 示例代码: package ma...

  • io 包的惯用接口速记

    io 包的常用接口速记 我没有 C/C++ 基础,没有接口的概念,且从 Python 投奔而来,Python 的极简主义(一个结果往往只提供一个方法),让我在...

  • 代理服务扩充

    代理服务扩展 之前自己实现了一个代理服务,当时考虑的是只要支持SOCKS5就好了,因为我经常用CHROME,配合着SwitchySharp,体验还是很棒...

  • 文件的创造与打开

    文件的创建与打开 文件操作是个很重要的话题,使用也非常频繁,熟悉如何操作文件是必不可少的。Golang 对文件的支持是在 os package ...

热门推荐: