Android Beam 文件传输会将文件复制到接收设备上的一个特殊目录中。此外,它还会使用 Android 媒体扫描器扫描复制的文件,并将媒体文件条目添加到
响应数据显示请求
当 Android Beam 文件传输将文件复制到接收设备后,它会发布一条通知,其中包含一个具有
例如,以下代码段展示了如何添加可触发 Activity com.example.android.nfctransfer.ViewActivity 的 intent 过滤器:
android:name="com.example.android.nfctransfer.ViewActivity"
android:label="Android Beam Viewer" >
...
...
注意:Android Beam 文件传输不是 从内容 URI 中获取目录部分。
请求文件权限
如需读取 Android Beam 文件传输所复制到设备的文件,可以请求
注意:从 Android 4.2.2(API 级别 17)开始,只有在用户选择执行
由于您的应用可以控制其内部存储区域,因此您无需请求写入权限,即可将传输的文件复制到内部存储区域。
获取已复制文件的目录
Android Beam 文件传输会将单次传输中的所有文件都复制到接收设备上的一个目录中。Android Beam 文件传输通知发送的内容
如需获取 URI 的架构,请调用
Kotlin
class MainActivity : Activity() {
...
// A File object containing the path to the transferred files
private var parentPath: File? = null
...
/*
* 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 fun handleViewIntent() {
...
/*
* For ACTION_VIEW, the Activity is being asked to display data.
* Get the URI.
*/
if (TextUtils.equals(intent.action, Intent.ACTION_VIEW)) {
// Get the URI from the Intent
intent.data?.also { beamUri ->
/*
* Test for the type of URI, by getting its scheme value
*/
parentPath = when (beamUri.scheme) {
"file" -> handleFileUri(beamUri)
"content" -> handleContentUri(beamUri)
else -> null
}
}
}
...
}
...
}Java
public class MainActivity extends Activity {
...
// A File object containing the path to the transferred files
private File parentPath;
// Incoming Intent
private Intent intent;
...
/*
* 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
intent = getIntent();
String action = intent.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 = intent.getData();
/*
* Test for the type of URI, by getting its scheme value
*/
if (TextUtils.equals(beamUri.getScheme(), "file")) {
parentPath = handleFileUri(beamUri);
} else if (TextUtils.equals(
beamUri.getScheme(), "content")) {
parentPath = handleContentUri(beamUri);
}
}
...
}
...
}
从文件 URI 中获取目录
如果传入的 file: 前缀以外的所有 URI 内容)。从路径部分创建
Kotlin
...
fun handleFileUri(beamUri: Uri): File? =
// Get the path part of the URI
beamUri.path.let { fileName ->
// Create a File object for this filename
File(fileName)
// Get the file's parent directory
.parentFile
}
...Java
...
public File 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 the file's parent directory
return copiedFile.getParentFile();
}
...
从内容 URI 中获取目录
如果传入的
此外,您还可能会接收具有以下特征的传入
注意:对于 Android Beam 文件传输,如果第一个传入文件的 MIME 类型是“audio/*”、“image/*”或“video/*”,您会在
确定内容提供程序
如需确定您是否可以从内容 URI 检索文件目录,请确定与 URI 相关联的内容提供程序,具体方法为:调用
URI 用于由
任何其他授权值
来自其他内容提供程序的内容 URI。显示与该内容 URI 相关联的数据,但不获取文件目录。
如需获取
以下代码段展示了如何测试内容 URI 的授权并检索已传输文件的路径和文件名:
Kotlin
...
private fun handleContentUri(beamUri: Uri): File? =
// Test the authority of the URI
if (beamUri.authority == MediaStore.AUTHORITY) {
/*
* Handle content URIs for other content providers
*/
...
// For a MediaStore content URI
} else {
// Get the column that contains the file name
val projection = arrayOf(MediaStore.MediaColumns.DATA)
val pathCursor = contentResolver.query(beamUri, projection, null, null, null)
// Check for a valid cursor
if (pathCursor?.moveToFirst() == true) {
// Get the column index in the Cursor
pathCursor.getColumnIndex(MediaStore.MediaColumns.DATA).let { filenameIndex ->
// Get the full file name including path
pathCursor.getString(filenameIndex).let { fileName ->
// Create a File object for the filename
File(fileName)
}.parentFile // Return the parent directory of the file
}
} else {
// The query didn't work; return null
null
}
}
...Java
...
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 copiedFile.getParentFile();
} else {
// The query didn't work; return null
return null;
}
}
}
...
如需详细了解如何从内容提供程序检索数据,请参阅从提供程序检索数据部分。
如需其他相关信息,请参阅: