Android最佳实践之分享文件

原文链接:Sharing a File

本课程教你:

  • 接收文件请求
  • 创建选取文件的Activity
  • 响应文件选取
  • 对选取的文件授权
  • 请求应用分享这个文件

一旦你设置了你的应用使用content URIs来分享文件,你就能够响应其他应用对这些文件的请求。响应这些请求的一种方式是从服务方应用提供一个文件选择接口供其他应用来调用。这种方式允许客户端应用用户从服务端应用选取文件,然后接收选中文件的content URI。

本课程教你如何在你的应用中根据请求的文件创建一个文件选取的 Activity

接收文件请求

为了接收来自客户端应用的请求以及返回一个content URI,你的应用需要提供一个文件选取 Activity。客户端应用使用包含 ACTION_PICK 动作的 Intent 调用 [startActivityForResult()](https://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)) 启动这个 Activity ,你的应用将根据用户选取的文件将对应的 content URI返回给客户端应用。

关于使用客户端应用发起请求的更多内容,请参见 Requesting a Shared File(中文链接:请求文件分享)。

创建一个文件选择Activity

为了设置一个选取文件的 Activity,在manifest文件中指定这个 Activity 名称,然后在intent filter中为起添加 ACTION_PICK 动作、 CATEGORY_DEFAULT策略以及 CATEGORY_OPENABLE 策略来匹配指定的选择事件。同样,添加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>

在代码中定义选取文件的Acitivity

下一步,定义一个 Activity 的子类,这个子类用来显示你的应用内部存储器files/images/目录下的可用文件。在这个目录下,用户可以选择他们想要的文件。下面这段代码显示了如何定义这个 Activity,以及如何根据用户选择做出相对应的响应:

public class MainActivity extends Activity {
    // The path to the root of this app's internal storage
    private File mPrivateRootDir;
    // The path to the "images" subdirectory
    private File mImagesDir;
    // Array of files in the images subdirectory
    File[] mImageFiles;
    // Array of filenames corresponding to mImageFiles
    String[] mImageFilenames;
    // Initialize the Activity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
       // Set up an Intent to send back to apps that request a file
       mResultIntent =
            new Intent("com.example.myapp.ACTION_RETURN_FILE");
      // Get the files/ subdirectory of internal storage
      mPrivateRootDir = getFilesDir();
      // Get the files/images subdirectory;
       mImagesDir = new File(mPrivateRootDir, "images");
      // Get the files in the images subdirectory
      mImageFiles = mImagesDir.listFiles();
      // Set the Activity's result to null to begin with
      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
       */
       ...
    }
    ...
}

响应文件选择

一旦用户选择了需要共享的文件,你的应用必须能够确定究竟是哪个文件被选择了,然后为这个文件产生一个content URI。因为 ActivityListView 控件中显示了所有可能的文件,当一个用户选择了一个文件时,系统就会调用 [onItemClick()](https://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html#onItemClick(android.widget.AdapterView<?>, android.view.View, int, long)) 方法,通过这个方法你能得到用户选择的文件。

在 [onItemClick()](https://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html#onItemClick(android.widget.AdapterView<?>, android.view.View, int, long)) 中,获取一个当前选中文件的 File 对象,将它与在 FileProvider<provider> 中指定的授权一起传递给 [getUriForFile()](https://developer.android.com/reference/android/support/v4/content/FileProvider.html#getUriForFile(android.content.Context, java.lang.String, java.io.File))。最终生成的content URI会包含这个授权、根据文件目录生成的路径段、以及文件的名字和扩展名。至于 FileProvider 怎样根据路径段映射到对应的文件目录,取决于在XML meta-data中的描述。详情请参考 Specify Sharable Directories(中文链接:Android最佳实践之设置文件分享——指定可分享的目录)。

以下的代码段教你怎样监测选中的文件以及如何获取它的content URI:

 protected void onCreate(Bundle savedInstanceState) {
    ...
    // Define a listener that responds to clicks on a file in the 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
         */
        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.
             */
            File requestFile = new File(mImageFilename[position]);
            /*
             * Most file-related method calls need to be in
             * try-catch blocks.
             */
            // Use the FileProvider to get a 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);
            }
            ...
        }
    });
    ...
}

注意你只能对providermeta-data文件下<paths>元素包含的文件路径生成相对应的 content URIs,正如 Specify Sharable Directories (中文链接:Android最佳实践之设置文件分享——指定可分享的目录)章节所说的那样。如果你调用 [getUriForFile()](https://developer.android.com/reference/android/support/v4/content/FileProvider.html#getUriForFile(android.content.Context, java.lang.String, java.io.File)) 传入一个未定义的文件路径,你会收到 IllegalArgumentException 异常。

为文件授权

现在你已经有了希望分享给其他应用的content URI,你需要允许客户端应用访问这个文件。为了允许访问这个文件,我们在 Intent 文件中添加这个文件的content URI并设置访问权限标志。你允许的权限是临时的,当接收方的应用任务栈结束的时候这个权限会自动到期。

下面的代码教你对这个文件如何设置读权限:

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
                mResultIntent.addFlags(
                    Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }
            ...
         }
         ...
    });
...
}

注意:在使用临时访问权限中,调用 setFlags() 是安全访问你的文件的唯一方式。避免调用 [Context.grantUriPermission()](https://developer.android.com/reference/android/content/Context.html#grantUriPermission(java.lang.String, android.net.Uri, int)) 方法来获取一个文件的content URI,因为这个方法会一直允许访问,直到你调用 [Context.revokeUriPermission()](https://developer.android.com/reference/android/content/Context.html#revokeUriPermission(android.net.Uri, int)) 方法移除这个权限为止。

使用请求应用共享这个文件

为了分享这个文件到请求的应用,在setResult() 中传送一个包含content URI和权限的 Intent。当你定义的这个 Activity 结束的时候,系统会发送这个包含content URI的 Intent 给客户端应用。 下面这段代码教你怎样完成这些操作:

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);
                }
            }
    });

一旦用户选择一个文件提供给用户一种立即返回客户端应用的方式。一种方法是提供一个复选标记或者完成按钮。在按钮上使用 android:onClick 属性绑定一个方法,在这个方法里调用 finish()。例如:

public void onDoneClick(View v) {
    // Associate a method with the Done button
    finish();
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容