注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好。

原文链接:http://developer.android.com/training/secure-file-sharing/request-file.html


当一个希望访问由其它应用所共享的文件时,需求应用(即客户端)经常会向其它应用(服务端)发送一个文件需求。在大多数情况下,这个需求会在服务端应用启动一个Activity显示可以共享的文件。当服务应用向客户应用返回了URI后,用户即选择了文件。

这节课向你展示一个客户应用如何向服务应用需求一个文件,接受服务应用发来的URI,然后使用这个URI打开这个文件。


一). 发送一个文件需求

为了向服务应用发送文件需求,在客户应用,需要调用startActivityForResult,同时传递给这个方法一个Intent,它包含了客户应用能处理的某行为,比如ACTION_PICK;以及一个MIME类型。

例如,下面的代码展示了如何向服务端应用发送一个Intent,来启动在Sharing a File(博客链接:http://www.cnblogs.com/jdneo/p/3480677.html)中提到的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);
...
}
...
}

二). 访问需求的文件

当服务应用向客户应用发回包含URI的Intent时,这个Intent会传递给客户应用的覆写的onActivityResult()方法当中。一旦客户应用有了文件的URI,它就可以通过获取其FileDescriptor来访问文件。

文件的安全问题在这一过程中不用过多担心,因为这个客户应用所受到的所有数据只有URI而已。由于URI不包含目录路径,客户应用无法查询出或者打开任何服务应用的其他文件。客户应用仅仅获取了这个文件的访问渠道和访问的权限。同时访问权限是临时的,一旦这个客户应用的任务栈被完成了,这个文件将只能被服务应用访问。

下面的例子展示了客户应用如何处理发自服务应用的Intent,以及如何客户应用使用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
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();
...
}
}

方法openFileDescriptor()返回一个文件的ParcelFileDescriptor。从这个对象中,客户应用可以获取FileDescriptor对象,然后用户就可以利用这个对象读取这个文件。

【Android Developers Training】 38. 文件共享需求的更多相关文章

  1. 【Android Developers Training】 36. 设置文件共享

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  2. 【Android Developers Training】 35. 序言:分享文件

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  3. 【Android Developers Training】 108. 使用模拟定位进行测试

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  4. 【Android Developers Training】 96. 运行一个同步适配器

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  5. 【Android Developers Training】 95. 创建一个同步适配器

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  6. 【Android Developers Training】 75. 使用NSD

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  7. 【Android Developers Training】 56. 更效率地加载大图片

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  8. 【Android Developers Training】 45. 控制音频焦点

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  9. 【Android Developers Training】 42. 从另一台设备接收文件

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

随机推荐

  1. Redis学习-String

    命令  描述  复杂的  返回值 SET key value [EX seconds] [PX milliseconds] [NX|XX] 将字符串值value关联到key.如果key已经持有其他值, ...

  2. hive的表的基本操作

    环境简介 实验环境使用的是cloudera-quickstart-vm-5.0环境. 内容摘要 创建表 修改表名 修改表中的列名 添加列 删除列 替换列 正文 Alter Table 语句 上面所述的 ...

  3. Cassandra Issue with Tombstone

    1. Cassandra is quicker than postgre and have lower change to lose data. Cassandra doesn't have fore ...

  4. 130行C语言实现个用户态线程库——ezthread

    准确的说是除掉头文件,测试代码和非关键的纯算法代码(只有双向环形链表的ADT),核心代码只有130行左右,已经是蝇量级的用户态线程库了.把这个库取名为ezthread,意思是,这太easy了,人人都可 ...

  5. python求职Top10城市,来看看是否有你所在的城市

    前言 从智联招聘爬取相关信息后,我们关心的是如何对内容进行分析,获取用用的信息. 本次以上篇文章“5分钟掌握智联招聘网站爬取并保存到MongoDB数据库”中爬取的数据为基础,分析关键词为“python ...

  6. MindNode for mac 思维导图

    绘制思维导图 下载地址 链接: https://pan.baidu.com/s/1o7NBzmU 密码: mu3f

  7. Java基础知识二次学习--第五章 数组

    第五章 数组 时间:2017年4月26日15:11:30~2017年4月26日15:15:54 章节:05章_01节  视频长度:09:30 内容:一维数组的内存分析 心得: Java中数组是引用类型 ...

  8. 第6章 影响 MySQL Server 性能的相关因素

    前言: 大部分人都一致认为一个数据库应用系统(这里的数据库应用系统概指所有使用数据库的系统)的性能瓶颈最容易出现在数据的操作方面,而数据库应用系统的大部分数据操作都是通过数据库管理软件所提供的相关接口 ...

  9. 一个可以控制提示框显示为top,bottom,left,right的小方法

    html代码 <!doctype html><html><head><meta charset="utf-8"><title& ...

  10. ReactNative学习之Html基础

    前言: React Native开发作为一种新型的移动开发方式,个人觉得App的一部分需求会逐步替换成这种方式,也是公司移动开发人员所必须掌握的一种开发技术,所以鉴于这种情况我觉得很有必要学习一下,特 ...