Note: I am a non-professional translator. I only write this blog for self-study. If you have any questions, please refer to the official blog.
When an application wants to access files shared by other applications, the requesting app (client) usually sends a request to the app of the shared file (server). In most cases, this request initiates an activity in the server application to display files that can be shared. The user selects a file, and then the server application returns the content URI of the file to the client application.
This course shows you how client applications request files from server applications, receive content URIs from server applications, and open files using content URIs.
I. Sending Document Request
To request a file from a server application, the client application invokes the startActivityForResult method containing actions such as ACTION_PICK that the server app can handle and a MIME-type intent.
For example, the following code snippet demonstrates how to send Intent to a server application to start the activities described in the shared file:
public class MainActivity extends Activity { private Intent mRequestFileIntent; private ParcelFileDescriptor mInputPFD; ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRequestFileIntent = new Intent(Intent.ACTION_PICK); mRequestFileIntent.setType("image/jpg"); ... } ... protected void requestFile() { /** * When the user requests a file, send an Intent to the * server app. * files. */ startActivityForResult(mRequestFileIntent, 0); ... } ... }
II. Access Request Documents
The server application sends the content URI of the file to the client application in Intent. This intent is passed to the client application in its overActivityResult () rewrite method. Once the client application has the content URI of the file, it can access the file by obtaining its FileDescriptor.
File security is preserved in this process because the content URI is the only data that the client application receives. Because this URI does not contain directory paths, client applications cannot find and open any other files in server applications. Only the client application can access the file, and only the permissions granted by the server application can be granted. Permissions are temporary, so once the task stack of the client application is completed, files cannot be accessed outside the server application.
The next code section demonstrates how the client application handles Intent sent from the server application and how the client application uses the content URI to obtain FileDescriptor:
/* * 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. */ mInputPFD = 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 = mInputPFD.getFileDescriptor(); ... } }
Method openFileDescriptor () returns the ParcelFileDescriptor of the file. With this object, the client application obtains a FileDescriptor object, which can then be used to read the file.