当应用想要访问另一个应用共享的文件时,请求应用(客户端)通常会向共享文件的应用(服务器)发送请求。在大多数情况下,请求会启动服务器应用中显示其可以共享的文件的Activity
。用户选择一个文件,之后服务器应用将文件的 content URI 返回给客户端应用。
本课程向您展示客户端应用如何从服务器应用请求文件,如何从服务器应用接收文件的 content URI,以及如何使用 content URI 打开文件。
发送文件请求
要从服务器应用请求文件,客户端应用会调用startActivityForResult
,并使用包含操作(例如ACTION_PICK
)和客户端应用可以处理的 MIME 类型的Intent
。
例如,以下代码片段演示了如何向服务器应用发送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); ... } ... }
访问请求的文件
服务器应用会将文件的 content URI 通过Intent
返回给客户端应用。此Intent
将在客户端应用的onActivityResult()
覆盖中传递给客户端应用。一旦客户端应用拥有文件的 content URI,它就可以通过获取其FileDescriptor
来访问该文件。
只有在您正确解析客户端应用接收到的 content 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
对象,然后可以使用它读取文件。
有关其他相关信息,请参阅