Sharing Files with NFC ->Sending Files to Another Device
来源:互联网 发布:淘宝大数据 实体店 编辑:程序博客网 时间:2024/06/03 19:36
如果通过Android Beam file transfer将大量的文件从一个device发送到另外一个device呢?
首先在manifest文件中定义NFC的权限和feature.
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc"
android:required="true" />
android:required="true" 也可以设定为fals,这样的话就需要在runtime时来判断hw是否support NFC
code如下:
PackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)
定义使用外部存储的权限。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Android Beam file transfer不support FileProvider.getUriForFile返回的URI。因此我们必须实现一个回调函数来返回Uri
这个回调函数是NfcAdapter.CreateBeamUrisCallback中的 createBeamUris()。
cide如下:
public class MainActivity extends Activity {
...
// List of URIs to provide to Android Beam
private Uri[] mFileUris = new Uri[10];
...
/**
* Callback that Android Beam file transfer calls to get
* files to share
*/
private class FileUriCallback implements
NfcAdapter.CreateBeamUrisCallback {
public FileUriCallback() {
}
/**
* Create content URIs as needed to share with another device
*/
@Override
public Uri[] createBeamUris(NfcEvent event) {
return mFileUris;
}
}
...
}
然后调用 setBeamPushUrisCallback() 来设定callback ,例子如下:
public class MainActivity extends Activity {
...
// Instance that returns available files from this app
private FileUriCallback mFileUriCallback;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Android Beam file transfer is available, continue
...
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
/*
* Instantiate a new FileUriCallback to handle requests for
* URIs
*/
mFileUriCallback = new FileUriCallback();
// Set the dynamic callback for URI requests.
mNfcAdapter.setBeamPushUrisCallback(mFileUriCallback,this);
...
}
...
}
如果指定要发送文件的uri呢?
/*
* Create a list of URIs, get a File,
* and set its permissions
*/
private Uri[] mFileUris = new Uri[10];
String transferFile = "transferimage.jpg";
File extDir = getExternalFilesDir(null);
File requestFile = new File(extDir, transferFile);
requestFile.setReadable(true, false);
// Get a URI for the File and add it to the list of URIs
fileUri = Uri.fromFile(requestFile);
if (fileUri != null) {
mFileUris[0] = fileUri;
} else {
Log.e("My Activity", "No File URI available for file.");
}
首先在manifest文件中定义NFC的权限和feature.
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc"
android:required="true" />
android:required="true" 也可以设定为fals,这样的话就需要在runtime时来判断hw是否support NFC
code如下:
PackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)
定义使用外部存储的权限。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Android Beam file transfer不support FileProvider.getUriForFile返回的URI。因此我们必须实现一个回调函数来返回Uri
这个回调函数是NfcAdapter.CreateBeamUrisCallback中的 createBeamUris()。
cide如下:
public class MainActivity extends Activity {
...
// List of URIs to provide to Android Beam
private Uri[] mFileUris = new Uri[10];
...
/**
* Callback that Android Beam file transfer calls to get
* files to share
*/
private class FileUriCallback implements
NfcAdapter.CreateBeamUrisCallback {
public FileUriCallback() {
}
/**
* Create content URIs as needed to share with another device
*/
@Override
public Uri[] createBeamUris(NfcEvent event) {
return mFileUris;
}
}
...
}
然后调用 setBeamPushUrisCallback() 来设定callback ,例子如下:
public class MainActivity extends Activity {
...
// Instance that returns available files from this app
private FileUriCallback mFileUriCallback;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Android Beam file transfer is available, continue
...
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
/*
* Instantiate a new FileUriCallback to handle requests for
* URIs
*/
mFileUriCallback = new FileUriCallback();
// Set the dynamic callback for URI requests.
mNfcAdapter.setBeamPushUrisCallback(mFileUriCallback,this);
...
}
...
}
如果指定要发送文件的uri呢?
/*
* Create a list of URIs, get a File,
* and set its permissions
*/
private Uri[] mFileUris = new Uri[10];
String transferFile = "transferimage.jpg";
File extDir = getExternalFilesDir(null);
File requestFile = new File(extDir, transferFile);
requestFile.setReadable(true, false);
// Get a URI for the File and add it to the list of URIs
fileUri = Uri.fromFile(requestFile);
if (fileUri != null) {
mFileUris[0] = fileUri;
} else {
Log.e("My Activity", "No File URI available for file.");
}
0 0
- Sharing Files with NFC ->Sending Files to Another Device
- Sharing Files with NFC ->Receiving Files from Another Device
- Sharing Files with NFC
- android-sharing files with NFC
- Sharing Files
- Sharing Files
- Sharing Files and Memory
- Sharing Files笔记
- How to Hack Windows 7: Sending Vulnerable Shortcut Files
- Sharing Files between RHEL 6 and Windows Systems with Samba
- How to map asm files with online database files
- Sending large files over JMS
- Sending binary files via minicom
- Understanding Disk Device Files
- base-kernel-device files
- 7.1. Talking to Device Files (writes and IOCTLs)}
- Upload files by sending multipart request programmatically
- Upload files by sending multipart request programmatically
- 在OpenStack(Mitaka版本)上通过Sahara部署Hadoop&Spark集群
- UITableViewCell高度自适应
- jquery attr()方法
- HDU 1009FatMouse' Trade
- Python制作字母验证码
- Sharing Files with NFC ->Sending Files to Another Device
- 我所认识的EXT2(二)
- Java入门_学习笔记[4]_继承
- leetcode 374 Guess Number Higher or Lower C++
- leetcode 383. Ransom Note
- HDU 1491 Octorber 21st
- Java序列化时,静态的成员变量 "可以被拿到数据" 是因为 类加载器之前就把这个类加载好了,拿到的是加载好的数据
- 字体属性及测量(FontMetrics)
- 单例模式(懒汉,饿汉)