您的位置:首页 > 移动开发 > Android开发

Google Android开发者文档系列-创建有内容分享特性的应用之请求共享文件

2016-06-06 17:45 507 查看

Requesting a Shared File(请求共享文件)

该系列文章是我在学习Google开发者文档时结合谷歌翻译和自身理解编写的,希望对学习Android开发的朋友带来些便利,由于个人翻译水平有限,所以内容包含原文和译文,希望浏览者结合理解,以免步入我可能错译的误区。在此感谢http://android.xsoftlab.net/提供的镜像,希望转载者注明出处/article/11851917.html方便查看最新博客

When an app wants to access a file shared by another app, the requesting app (the client) usually sends a request to the app sharing the files (the server). In most cases, the request starts an Activity in the server app that displays the files it can share. The user picks a file, after which the server app returns the file’s content URI to the client app.

当一个app想要使用一个其他app共享的文件时,请求的app(客户端)经常发送一个请求到分享文件的app(服务端)。在大部分情况下,请求会启动服务端app的一个activity来显示它可以共享的文件列表。用户选择一个文件,然后客户端app会返回该文件的content URI给客户端app。

This lesson shows you how a client app requests a file from a server app, receives the file’s content URI from the server app, and opens the file using the content URI.

这节课将展示客户端app如何向服务端app请求一个文件,从服务端app接收一个content URI,并通过该content URI打开对应的文件。

Send a Request for the File(发送一个共享文件请求)

To request a file from the server app, the client app calls startActivityForResult with an Intent containing the action such as ACTION_PICK and a MIME type that the client app can handle.

要从服务端app请求文件,客户端app需要调用startActivityForResult()方法,并向其传入一个包含ACTION_PICK 的action和客户端app可以处理文件类型的MIME类型的intent。

For example, the following code snippet demonstrates how to send an Intent to a server app in order to start the Activity described in Sharing a File:

例如,下面的代码片段演示了如何将intent发送到服务器app,以启动一个Sharing a File中描述的activity:

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


Access the Requested File(使用请求的文件)

The server app sends the file’s content URI back to the client app in an Intent. This Intent is passed to the client app in its override of onActivityResult(). Once the client app has the file’s content URI, it can access the file by getting its FileDescriptor.

服务端app通过intent发送文件的content URI给客户端app。传给客户端app的intent在它的onActivityResult()方法中。一旦客户端app得到了文件的content URI,它就能够通过获取文件的FileDescriptor来使用该文件。

File security is preserved in this process because the content URI is the only piece of data that the client app receives. Since this URI doesn’t contain a directory path, the client app can’t discover and open any other files in the server app. Only the client app gets access to the file, and only for the permissions granted by the server app. The permissions are temporary, so once the client app’s task stack is finished, the file is no longer accessible outside the server app.

文件的安全性是在这个过程中保存下来,因为content URI是客户端app接收数据的唯一一块。由于这个URI不包含目录路径,客户端app无法发现并在服务器应用中打开的任何其他文件。只有客户端app获得访问该文件,和只针对服务器app授予的权限。权限是临时的,所以一旦客户端app的任务堆栈完成后,该文件在服务器app外部将不可访问。

The next snippet demonstrates how the client app handles the Intent sent from the server app, and how the client app gets the FileDescriptor using the content URI:

下面的代码块示范了客户端app如何操作服务端app发送的intent,和客户端app如何通过content URI得到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
//获取得到的intent包含的文件的content URI数据
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();
...
}
}


The method openFileDescriptor() returns a ParcelFileDescriptor for the file. From this object, the client app gets a FileDescriptor object, which it can then use to read the file.

该方法openFileDescriptor()返回该文件的ParcelFileDescriptor。从这个对象,客户端app获取一个文件描述符对象,通过它可以读取文件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: