Android官方开发文档Training系列课程中文版:分享文件之分享一个文件

简介: 原文地址:http://android.xsoftlab.net/training/secure-file-sharing/share-file.html一旦APP设置通过URI的方式共享文件,你需要响应其它APP请求这些文件的请求。

原文地址:http://android.xsoftlab.net/training/secure-file-sharing/share-file.html

一旦APP设置通过URI的方式共享文件,你需要响应其它APP请求这些文件的请求。响应这些请求的一种方式是,在服务端APP上提供一个文件选择接口,以便其它的程序可以调用。这种方法允许客户端程序的用户从服务端选择一个文件,然后接收被选择文件的URI地址。

这节课展示了如何在Activity中创建文件选择功能,以便响应文件请求。

接收文件请求

为了接收客户端APP的文件请求,以及以URI的方式作出响应,APP应该在Activity中提供文件选择器。这样的话,客户端APP可以通过调用startActivityForResult()方法启动这个Activity。当客户端调用了startActivityForResult()方法,你的APP可以返回一个结果给客户端APP,这个结果以URI的形式将用户选择的文件返回。

有关学习如何在客户端APP中实现文件的请求,请参见课程: Requesting a Shared File

创建一个文件选择器Activity

为了设置文件选择器Activity,首选需要在清单文件中指定activity,并在其中附加意图过滤器,这个意图过滤器用来匹配行为ACTION_PICK以及类别CATEGORY_DEFAULTCATEGORY_OPENABLE。还要给服务端给客户端提供的文件添加MIME类型过滤器。下面的代码片段展示了如何指定一个新Activity以及过滤器:

<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>

在代码中定义文件选择器

接下来,定义一个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
         */
         ...
    }
    ...
}

响应文件选择器

一旦用户选择了被共享的文件,你的程序必须检查哪一个文件被选中,并且生成该文件的URI地址。前面的部分Activity在ListView中展示了可用的文件列表,当用户点击了文件的名称,随之系统会调用方法onItemClick(),在这个方法中你可以获取到被选中的文件。

onItemClick()方法中,从被选择的文件中获得文件的File对象,然后将这个对象作为参数传递给getUriForFile(),它会伴随着权限一并加入,这个权限是由 < provider>元素指定的。结果URI会包含权限、文件在相应目录中的路径段(在XML meta-data中指定的部分)以及文明的名称和其扩展部分。有关FileProvider如何映射meta-data与路径段的目录,请看章节Specify Sharable Directories

下面的代码段展示了如何检测被选择的文件以及获取该文件的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);
                }
                ...
            }
        });
        ...
    }

要记住,你只可以对在< paths>元素中指定目录下的文件进行URI编码,就像Specify Sharable Directories这节课中描述的一样。如果你对getUriForFile()方法中传入的File参数并没有在< path>元素中指定,那么你会收到一个 IllegalArgumentException异常。

对文件授予权限

现在对将要分享的文件有了一个URI,你需要允许客户端APP来访问这个文件。为了允许访问,需要通过添加URI到一个Intent上,并且在这个Intent上设置权限标志。产生的这个权限是个临时权限,并且会在接收端APP任务结束的时候自动终止。

下面这段代码展示了如何对文件设置读取权限:

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

Caution:安全的对文件授予临时访问权限,setFlags()是唯一的方式。要避免对文件的URI使用Context.grantUriPermission()方法,因为该方法授予的权限只可以通过Context.revokeUriPermission()方法撤销。

对客户端APP共享文件

如果要共享文件给客户端APP,需要传递一个Intent给setResult(),这个Intent包含了文件的URI以及访问权限。当你定义的这个Activity结束时,系统会发送这个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);
                    }
                }
        });

一旦用户选择了文件,那么就应该立即带用户返回客户端APP。实现这种方式的一种方法就是提供一个钩形符号或者结束按钮。使用Button的android:onClick属性与对应的Button相关联。在方法中。调用finish()方法:

    public void onDoneClick(View v) {
        // Associate a method with the Done button
        finish();
    }
目录
相关文章
|
2月前
|
Android开发
安卓SO层开发 -- 编译指定平台的SO文件
安卓SO层开发 -- 编译指定平台的SO文件
29 0
|
4月前
|
XML Java Android开发
Android App开发网络通信中使用okhttp下载和上传图片、文件讲解及实战(超详细实现用户注册信息上传 附源码)
Android App开发网络通信中使用okhttp下载和上传图片、文件讲解及实战(超详细实现用户注册信息上传 附源码)
128 0
|
24天前
|
Java Android开发
Android开发系列全套课程
本系列课程面向有java基础,想进入企业从事android开发的计算机专业者。学习搭配实战案例,高效掌握岗位知识。
16 1
|
2月前
|
JSON Java Go
|
2月前
|
算法 Java Android开发
安卓逆向 -- 调用其他APK的SO文件
安卓逆向 -- 调用其他APK的SO文件
17 0
|
2月前
|
Android开发
安卓逆向 -- Hook多个dex文件
安卓逆向 -- Hook多个dex文件
18 1
|
3月前
|
IDE 开发工具 Android开发
Android Studio 下发布项目成APK文件
Android Studio 下发布项目成APK文件
117 1
|
3月前
|
安全 Android开发 数据安全/隐私保护
安卓逆向 -- SO文件逆向分析
安卓逆向 -- SO文件逆向分析
19 0
|
3月前
|
存储 前端开发 测试技术
Android 官方架构中的 UseCase 该怎么写?
Android 官方架构中的 UseCase 该怎么写?
65 0
|
4月前
|
XML JSON Apache
【Android】如何获得Apache服务器的JSON文件数据
【Android】如何获得Apache服务器的JSON文件数据
56 0