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

原文链接:http://developer.android.com/training/beam-files/receive-files.html


Android Beam文件传输将文件拷贝至接收设备上的一个特殊目录。同时使用Android媒体扫描器(Android Media Scanner)扫描拷贝的文件,并为媒体文件添加对应的字段至媒体库(MediaStore provide)中。这节课将向你展示当文件拷贝完成时要如何响应,并且在接收设备上应该如何放置拷贝的文件。


一). 响应需求以显示数据

当Android Beam文件传输将文件拷贝至接收设备后,它会发布一个通知,包含了一个Intent,它有有一个ACTION_VIEW的行为,第一个传输文件的MIME类型,和一个指向第一个文件的URI。当用户点击了这个通知后,intent会被发送至系统。为了让你的应用能够响应这个intent,为要响应的Activity所对应的<activity>标签添加<intent-filter>标签,在<intent-filter>标签中,添加下面的子标签:

<action android:name="android.intent.action.VIEW" />

用来匹配从通知发送的intent。

<category android:name="android.intent.category.CATEGORY_DEFAULT" />

匹配隐式的Intent

<data android:mimeType="mime-type" />

匹配一个MIME类型。要指定那些你的应用能够处理的类型。

例如,下面的例子展示了如何添加一个intent过滤器来激活你的activity:

com.example.android.nfctransfer.ViewActivity

<activity
android:name="com.example.android.nfctransfer.ViewActivity"
android:label="Android Beam Viewer" >
...
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
...
</intent-filter>
</activity>

Note:

不仅仅只有Android Beam文件传输会发送含有ACTION_VIEW的intent。在接收设备上的其它应用也有可能会发送含有该行为的intent。我们马上会进一步讨论这一问题。


二). 需求文件权限

如果要读取Android Beam文件传输所拷贝到设备上的文件,需要READ_EXTERNAL_STORAGE权限。例如:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

如果你希望将文件拷贝指你自己应用的存储区,那么需要的权限改为WRITE_EXTERNAL_STORAGE,另外WRITE_EXTERNAL_STORAGE权限包含了READ_EXTERNAL_STORAGE权限。

Note:

对于Android 4.2.2 (API Level 17),READ_EXTERNAL_STORAGE权限仅在用户选择要读文件时才是强制需要的。而在今后的版本中会在所有情况下都需要该权限。为了保证应用在未来的兼容性,建议在清单文件中申明该权限。

由于你的应用对于其内部存储区域具有控制权,所以若要将文件拷贝至你应用的内部存储区域,写权限式不需要申明的。


三). 获取拷贝文件的目录

Android Beam文件传输一次性将所有文件拷贝到目标设备的一个目录内,Android Beam文件传输通知所发出的Intent中包含有URI,他指向了第一个传输的文件。然而,你的应用也有可能接收到除了Android Beam文件传输之外的某个来源所发出的含有ACTION_VIEW行为的Intent。为了明确你应该如何处理接收的Intent,你需要检查它的架构(scheme)和权威(authority)。

为了获得URI的架构,调用Uri.getScheme(),下面的代码展示了如何明确架构并处理URI:

public class MainActivity extends Activity {
...
// A File object containing the path to the transferred files
private File mParentPath;
// Incoming Intent
private Intent mIntent;
...
/*
* Called from onNewIntent() for a SINGLE_TOP Activity
* or onCreate() for a new Activity. For onNewIntent(),
* remember to call setIntent() to store the most
* current Intent
*
*/
private void handleViewIntent() {
...
// Get the Intent action
mIntent = getIntent();
String action = mIntent.getAction();
/*
* For ACTION_VIEW, the Activity is being asked to display data.
* Get the URI.
*/
if (TextUtils.equals(action, Intent.ACTION_VIEW)) {
// Get the URI from the Intent
Uri beamUri = mIntent.getData();
/*
* Test for the type of URI, by getting its scheme value
*/
if (TextUtils.equals(beamUri.getScheme(), "file")) {
mParentPath = handleFileUri(beamUri);
} else if (TextUtils.equals(
beamUri.getScheme(), "content")) {
mParentPath = handleContentUri(beamUri);
}
}
...
}
...
}

从文件URI中获取目录

如果接收的Intent包含一个内容URI,则该URI包含了一个文件的绝对文件名,包括了完整的路径和文件名。对于Android Beam文件传输来说,目录路径指向了其它传输文件的位置(如果有其它传输文件的话),要获得这个目录路径,取得URI的路径部分(URI中除去“file:”前缀的部分),根据路径创建一个File对象,然后获取这个File的父目录:

...
public String handleFileUri(Uri beamUri) {
// Get the path part of the URI
String fileName = beamUri.getPath();
// Create a File object for this filename
File copiedFile = new File(fileName);
// Get a string containing the file's parent directory
return copiedFile.getParent();
}
...

从内容URI获取目录

如果接收的Intent包含一个内容URI,这个URI可能指向的是一个存储于MediaStore内容提供程序的目录和文件名。你可以通过检测URI的权威值来判断是否是MediaStore的内容URI。一个MediaStore的内容URI可能来自Android Beam文件传输也可能来自其它应用,但不管怎么样,你都能根据该内容URI获得一个目录和文件名。

你也能接收一个ACTION_VIEW的Intent,它包含有一个内容提供程序的URI而不是MediaStore,在这个例子中,这个内容URI不包含MediaStore的权威值,且这个URI一般不指向一个目录

Note:

对于Android Beam文件传输,如果第一个接收的文件,其MIME类型为“audio/*”,“image/*”或者“video/*”,那么你接收这个处于ACTION_VIEW的Intent的内容URI。Android Beam文件传输会通过在它存储传输文件的目录内运行媒体扫描器,以此为媒体文件添加索引。同时媒体扫描器将结果写入MediaStore内容提供程序,之后它将第一个文件的内容URI回递给Android Beam文件传输。这个内容URI就是你在通知Intent中所接收到的。要获得第一个文件的目录,你需要使用该内容URI从MediaStore中获取它。

确定内容提供程序

为了明确你能从内容URI中获取文件目录,你可以通过调用Uri.getAuthority()获取URI的权威,以此确定与该URI相关联的内容提供程序。其结果有两个可能的值:

MediaStore.AUTHORITY

表明这个URI关联了被MediaStore追踪的一个文件或者多个文件。可以从MediaStore中获取文件的全名,目录名就自然可以从文件全名中获取。

其他任何权威值

来自其他内容提供程序的内容URI。可以显示与该内容URI相关联的数据,但是不要尝试去获取文件目录。

为了从MediaStore的内容URI中获取目录,执行一个查询操作,它将Uri参数指定为收到的内容URI,列名为MediaColumns.DATA。返回的Cursor包含了完整路径和URI所代表的文件名。该目录路径下还包含了由Android Beam文件传输传送到该设备上的其它文件。

下面的代码展示了你要如何测试内容URI的权威值,并获取传输文件的路径和文件名:

 ...
public String handleContentUri(Uri beamUri) {
// Position of the filename in the query Cursor
int filenameIndex;
// File object for the filename
File copiedFile;
// The filename stored in MediaStore
String fileName;
// Test the authority of the URI
if (!TextUtils.equals(beamUri.getAuthority(), MediaStore.AUTHORITY)) {
/*
* Handle content URIs for other content providers
*/
// For a MediaStore content URI
} else {
// Get the column that contains the file name
String[] projection = { MediaStore.MediaColumns.DATA };
Cursor pathCursor =
getContentResolver().query(beamUri, projection,
null, null, null);
// Check for a valid cursor
if (pathCursor != null &&
pathCursor.moveToFirst()) {
// Get the column index in the Cursor
filenameIndex = pathCursor.getColumnIndex(
MediaStore.MediaColumns.DATA);
// Get the full file name including path
fileName = pathCursor.getString(filenameIndex);
// Create a File object for the filename
copiedFile = new File(fileName);
// Return the parent directory of the file
return new File(copiedFile.getParent());
} else {
// The query didn't work; return null
return null;
}
}
}
...

要学习更多关于从内容提供程序获取数据的知识,可以阅读:Retrieving Data from the Provider

【Android Developers Training】 42. 从另一台设备接收文件的更多相关文章

  1. 【Android Developers Training】 40. 序言:通过NFC共享文件

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

  2. 【Android Developers Training】 41. 向另一台设备发送文件

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

  3. 【Android Developers Training】 2. 运行你的应用

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

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

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

  5. 【Android Developers Training】 94. 创建一个空内容提供器(Content Provider)

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

  6. 【Android Developers Training】 92. 序言:使用同步适配器传输数据

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

  7. 【Android Developers Training】 91. 解决云储存冲突

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

  8. 【Android Developers Training】 89. 最大化的使用谷歌云消息(Google Cloud Messaging)

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

  9. 【Android Developers Training】 83. 实现高效网络访问来优化下载

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

随机推荐

  1. PL/SQL Developer使用技巧以及快捷键设置

    1.类SQL PLUS窗口: File->New->Command Window,这个类似于oracle的客户端工具sql plus,但是比在cmd中的sqlplus好用多了. 2.设置关 ...

  2. Java面试题之最扯淡的String

    public class ThreadException { public static void main(String[] args) { 没加final的代码 String hello = &q ...

  3. HDFS 架构简述

    HDFS 架构简述 Hadoop分布式文件系统(HDFS)是一个分布式的文件系统,运行在廉价的硬件上.它与现有的分布式文件系统有很多相似之处.然而与其他的分布式文件系统的差异也是显着的.HDFS是高容 ...

  4. 解决ssh或ftp下root用户认证失败问题

    问题:当连接ssh远程终端或使用ftp方式进行文件传输时,使用普通用户可以进行远程登录,但使用root用户则认证失败,提示密码错误.而我们在普通用户登录下,su - root,验证密码,是可以正常切换 ...

  5. 龟兔赛跑,杭电oj-2059

    原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=2059 [Problem Description] 据说在很久很久以前,可怜的兔子经历了人生中最大的打击 ...

  6. Windows下快速安装Xgboost(无需Git或者VS)

    xgboost的全称是eXtreme Gradient Boosting,现在已经风靡Kaggle.天池.DataCastle.Kesci等国内外数据竞赛平台,是比赛夺冠的必备大杀器!如果把数据竞赛比 ...

  7. Spring Controller单元测试

    SpringMVC controller测试较简单,从功能角度划分,可分为两种.一种是调用请求路径测试,另一种是直接调用Controller方法测试. 调用请求路径测试 通过请求路径调用,请求需要经过 ...

  8. Fiddler中如何抓取app中https(443端口)数据

    Fiddler不但能截获各种浏览器发出的HTTP请求, 也可以截获手机发出的HTTP/HTTPS请求,总结下Fiddler截获IPhone和Android发出的HTTP/HTTPS包,前提条件是:安装 ...

  9. MySQL主从同步报错故障处理集锦

    前言 在发生故障切换后,经常遇到的问题就是同步报错,下面是最近收集的报错信息. 记录删除失败 在master上删除一条记录,而slave上找不到 Last_SQL_Error: Could not e ...

  10. R – GPU Programming for All with ‘gpuR’

    INTRODUCTION GPUs (Graphic Processing Units) have become much more popular in recent years for compu ...