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();
    }
时间: 2025-01-12 11:55:59

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

Google Android开发者文档系列-与其他应用程序交互之使用户跳转到其它应用程序

Sending the User to Another App(使用户跳转到其它应用程序) 该系列文章是我在学习Google开发者文档时结合谷歌翻译和自身理解编写的,希望对学习Android开发的朋友带来些便利,由于个人翻译水平有限,所以内容包含原文和译文,希望浏览者结合理解,以免步入我可能错译的误区.在此感谢http://android.xsoftlab.net/提供的镜像,希望转载者注明出处http://blog.csdn.net/u014031072/article/details/515

离线使用Android开发者文档

Android开发者网站被墙了 N 久了,不翻墙又要看文档,很痛苦啊. 原本 Android SDK 里是带了完整的文档的,可以离线浏览.我的电脑上,SDK 位置是"E:\android-sdk-windows",这个目录下有个 docs 目录,docs 下面有个 index.html 文件,这就是 Android 开发文档的起始页了,打开它就能查到绝大部分的文档,比如各种类库.入门教程等等. 可是有个问题啊,如果你电脑连接了互联网,那就奇慢无比,貌似这些离线文档中的很多资源会引用网站

Android官方入门文档[1]创建一个Android项目

Android官方入门文档[1]创建一个Android项目 创建一个Android项目 这节课教你1.创建与Android Studio中的一个项目2.创建使用命令行工具项目 你也应该阅读?管理项目 Creating an Android Project This lesson teaches you to1.Create a Project with Android Studio2.Create a Project with Command Line Tools You should also

Android官方入门文档[16]创建一个Fragment代码片段

Android官方入门文档[16]创建一个Fragment代码片段 Creating a Fragment创建一个Fragment代码片段 This lesson teaches you to1.Create a Fragment Class2.Add a Fragment to an Activity using XML You should also read?Fragments 这节课教你1.创建一个Fragment代码片段类2.使用XML来添加一个Fragment代码片段给一个活动 你也

提高打开Android本地文档的速度

很多Android开发者在参考Android官方API时,都有一个令人头疼的问题:打开一个index.html平均都需要几分钟甚至更长,尤其是在打开API 8以上的版本的时候.难道是网速不够好?但笔者使用的是网通8M的宽带,所以基本上可以排除网速的问题.难道是浏览器的问题?笔者尝试分别使用IE11.谷歌浏览器.火狐浏览器.遨游等主流的浏览器来测试打开速度,发现都是一样慢的像蜗牛!最后,笔者无奈只好查看API页面源代码,发现页面加载的有这样两端代码: <!-- STYLESHEETS --> &

微信开发者文档

接收事件推送 目录 1 关注/取消关注事件 2 扫描带参数二维码事件 3 上报地理位置事件 4 自定义菜单事件 5 点击菜单拉取消息时的事件推送 6 点击菜单跳转链接时的事件推送 关注/取消关注事件 用户在关注与取消关注公众号时,微信会把这个事件推送到开发者填写的URL.方便开发者给用户下发欢迎消息或者做帐号的解绑. 微信服务器在五秒内收不到响应会断掉连接,并且重新发起请求,总共重试三次 关于重试的消息排重,推荐使用FromUserName + CreateTime 排重. 假如服务器无法保证在

解决android帮助文档打开慢

经查是因为本地文档中的网页有如下两段js代码会联网加载信息,将其注释掉后就好了 <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Roboto:regular,medium,thin,italic,mediumitalic,bold" title="roboto"> <script src="http://www.google.com

三种方法解决android帮助文档打开慢

三种方法解决android帮助文档打开慢 经查是因为本地文档中的网页有如下两段js代码会联网加载信息,将其注释掉后就好了 <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Roboto:regular,medium,thin,italic,mediumitalic,bold" title="roboto"> <script src="

加快Android离线文档的访问速度

OptAndroidDocs 背景: 自从Google被墙了之后,访问Android的API文档一直是个问题.虽然Android SDK可以将开发文档下载下来离线查看,但是由于文档中仍然链接Google的在线资源导(js,fonts等)致访问速度仍然很慢. 网上大概有2种方法: 采用脚本删掉html文件中对在线资源的引用(网上也有别人处理过的可以下载) 是脱机查看,有人还在chrome下写了个插件. 但是我对以上两种方法还不够满意: Android文档更新之后,需要重新处理.脚本很慢,等待网友提