当应用想要访问其他应用共享的文件时,发出请求的应用(客户端)
通常会向共享文件的应用(服务器)发送请求。在大多数情况下,
在服务器应用中启动一个 Activity
,以显示可共享的文件。
用户选择文件后,服务器应用会将文件的内容 URI 返回给
。
本课将介绍客户端应用如何从服务器应用请求文件,以及如何接收文件的 内容 URI,并使用内容 URI 打开文件。
发送文件请求
如需从服务器应用请求文件,客户端应用调用
startActivityForResult
,具有
Intent
包含如下操作:
ACTION_PICK
以及客户端应用
例如,以下代码段展示了如何发送
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
。此 Intent
会传递给客户端
替换 onActivityResult()
。一次
客户端应用具有文件的内容 URI,其可通过获取其
FileDescriptor
。
只有正确解析内容 URI 后,在此过程中才会保证文件安全性 唯一 ID解析内容时,必须确保此 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
对象,然后使用该对象读取文件。
如需了解其他相关信息,请参阅: