Android NDK开发详解应用数据和文件之请求某个分享的文件

Android NDK开发详解应用数据和文件之请求某个分享的文件

当应用想要访问其他应用共享的文件时,请求方应用(客户端)通常会向共享文件的应用(服务器)发送请求。在大多数情况下,请求会在服务器应用中启动一个 Activity,以显示可共享的文件。用户选择文件后,服务器应用会将文件的内容 URI 返回给客户端应用。

本课程介绍了客户端应用如何从服务器应用请求文件,如何从服务器应用接收文件的内容 URI,以及如何使用内容 URI 打开文件。
发送文件请求

为了从服务器应用请求文件,客户端应用需要使用 Intent 调用 startActivityForResult,该 Intent 包含 ACTION_PICK 等操作和客户端应用能够处理的 MIME 类型。

例如,以下代码段展示了如何向服务器应用发送 Intent 以启动共享文件中所述的 Activity:
Kotlin

class MainActivity : Activity() {
        private lateinit var requestFileIntent: Intent
        private lateinit var inputPFD: ParcelFileDescriptor
        ...
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            requestFileIntent = Intent(Intent.ACTION_PICK).apply {
                type = "image/jpg"
            }
            ...
        }
        ...
        private fun requestFile() {
            /**
             * When the user requests a file, send an Intent to the
             * server app.
             * files.
             */
            startActivityForResult(requestFileIntent, 0)
            ...
        }
        ...
    }
    

Java

 public class MainActivity extends Activity {
        private Intent requestFileIntent;
        private ParcelFileDescriptor inputPFD;
        ...
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            requestFileIntent = new Intent(Intent.ACTION_PICK);
            requestFileIntent.setType("image/jpg");
            ...
        }
        ...
        protected void requestFile() {
            /**
             * When the user requests a file, send an Intent to the
             * server app.
             * files.
             */
                startActivityForResult(requestFileIntent, 0);
            ...
        }
        ...
    }

访问请求的文件

服务器应用通过 Intent 将文件的内容 URI 发回给客户端应用。此 Intent 会通过替换 onActivityResult() 传递给客户端应用。客户端应用获得文件的内容 URI 后,即可通过获取其 FileDescriptor 来访问该文件。

由于客户端应用只会收到文件的内容 URI,因此,在此过程中可确保文件的安全。由于此 URI 中不包含目录路径,因此客户端应用无法发现和打开服务器应用中的任何其他文件。只有客户端应用可以访问该文件,而且必须具有服务器应用授予的权限才能访问。这些权限是临时的,一旦客户端应用的任务堆栈结束,便无法再从服务器应用外访问该文件。

以下代码段展示了客户端应用如何处理服务器应用发送的 Intent,以及如何使用内容 URI 获取 FileDescriptor:
Kotlin

  /*
     * When the Activity of the app that hosts files sets a result and calls
     * finish(), this method is invoked. The returned Intent contains the
     * content URI of a selected file. The result code indicates if the
     * selection worked or not.
     */
    public override fun onActivityResult(requestCode: Int, resultCode: Int, returnIntent: Intent) {
        // If the selection didn't work
        if (resultCode != Activity.RESULT_OK) {
            // Exit without doing anything else
            return
        }
        // Get the file's content URI from the incoming Intent
        returnIntent.data?.also { returnUri ->
            /*
             * Try to open the file for "read" access using the
             * returned URI. If the file isn't found, write to the
             * error log and return.
             */
            inputPFD = try {
                /*
                 * Get the content resolver instance for this context, and use it
                 * to get a ParcelFileDescriptor for the file.
                 */
                contentResolver.openFileDescriptor(returnUri, "r")
            } catch (e: FileNotFoundException) {
                e.printStackTrace()
                Log.e("MainActivity", "File not found.")
                return
            }

            // Get a regular file descriptor for the file
            val fd = inputPFD.fileDescriptor
            ...
        }
    }
    

Java

        /*
         * When the Activity of the app that hosts files sets a result and calls
         * finish(), this method is invoked. The returned Intent contains the
         * content URI of a selected file. The result code indicates if the
         * selection worked or not.
         */
        @Override
        public void onActivityResult(int requestCode, int resultCode,
                Intent returnIntent) {
            // If the selection didn't work
            if (resultCode != RESULT_OK) {
                // Exit without doing anything else
                return;
            } else {
                // Get the file's content URI from the incoming Intent
                Uri returnUri = returnIntent.getData();
                /*
                 * Try to open the file for "read" access using the
                 * returned URI. If the file isn't found, write to the
                 * error log and return.
                 */
                try {
                    /*
                     * Get the content resolver instance for this context, and use it
                     * to get a ParcelFileDescriptor for the file.
                     */
                    inputPFD = getContentResolver().openFileDescriptor(returnUri, "r");
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    Log.e("MainActivity", "File not found.");
                    return;
                }
                // Get a regular file descriptor for the file
                FileDescriptor fd = inputPFD.getFileDescriptor();
                ...
            }
        }

方法 openFileDescriptor() 会返回文件的 ParcelFileDescriptor。客户端应用从此对象获取 FileDescriptor 对象,然后使用后者来读取文件。

如需了解其他相关信息,请参阅:

Intent 和 Intent 过滤器
从提供器检索数据

本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。

最后更新时间 (UTC):2020-06-11。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五一编程

程序之路有我与你同行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值