page.title=Sending Files to Another Device trainingnavtop=true @jd:body
This lesson shows you how to design your app to send large files to another device using Android Beam file transfer. To send files, you request permission to use NFC and external storage, test to ensure your device supports NFC, and provide URIs to Android Beam file transfer.
The Android Beam file transfer feature has the following requirements:
First, edit your app manifest to declare the permissions and features your app needs.
To allow your app to use Android Beam file transfer to send files from external storage using NFC, you must request the following permissions in your app manifest:
<manifest>
element:
<uses-permission android:name="android.permission.NFC" />
<manifest>
element:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Note: As of Android 4.2.2 (API level 17), this permission is not enforced. Future versions of the platform may require it for apps that want to read from external storage. To ensure forward compatibility, request the permission now, before it becomes required.
Specify that your app uses NFC, by adding a
<uses-feature>
element as a child
of the <manifest>
element. Set the android:required
attribute to
true
to indicate that your app won't function unless NFC is present.
The following snippet shows you how to specify the
<uses-feature>
element:
<uses-feature android:name="android.hardware.nfc" android:required="true" />
Note that if your app only uses NFC as an option, but still functions if NFC isn't present, you
should set android:required
to false
, and test for NFC in code.
Since Android Beam file transfer is only available in Android 4.1 (API level 16) and later,
if your app depends on Android Beam file transfer for a key part of its functionality you must
specify the <uses-sdk>
element with the
android:minSdkVersion="16"
attribute. Otherwise, you can set
android:minSdkVersion
to another value as necessary, and test for the platform
version in code, as described in the following section.
To specify in your app manifest that NFC is optional, you use the following element:
<uses-feature android:name="android.hardware.nfc" android:required="false" />
If you set the attribute
android:required="false"
, you must test for NFC support and Android Beam file
transfer support in code.
To test for Android Beam file transfer support in code, start by testing that the device supports NFC by calling {@link android.content.pm.PackageManager#hasSystemFeature PackageManager.hasSystemFeature()} with the argument {@link android.content.pm.PackageManager#FEATURE_NFC FEATURE_NFC}. Next, check that the Android version supports Android Beam file transfer by testing the value of {@link android.os.Build.VERSION#SDK_INT}. If Android Beam file transfer is supported, get an instance of the NFC controller, which allows you to communicate with the NFC hardware. For example:
public class MainActivity extends Activity { ... NfcAdapter mNfcAdapter; // Flag to indicate that Android Beam is available boolean mAndroidBeamAvailable = false; ... @Override protected void onCreate(Bundle savedInstanceState) { ... // NFC isn't available on the device if (!PackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)) { /* * Disable NFC features here. * For example, disable menu items or buttons that activate * NFC-related features */ ... // Android Beam file transfer isn't supported } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { // If Android Beam isn't available, don't continue. mAndroidBeamAvailable = false; /* * Disable Android Beam file transfer features here. */ ... // Android Beam file transfer is available, continue } else { mNfcAdapter = NfcAdapter.getDefaultAdapter(this); ... } } ... }
Once you've verified that the device supports Android Beam file transfer, add a callback method that the system invokes when Android Beam file transfer detects that the user wants to send files to another NFC-enabled device. In this callback method, return an array of {@link android.net.Uri} objects. Android Beam file transfer copies the files represented by these URIs to the receiving device.
To add the callback method, implement the {@link android.nfc.NfcAdapter.CreateBeamUrisCallback} interface and its method {@link android.nfc.NfcAdapter.CreateBeamUrisCallback#createBeamUris createBeamUris()}. The following snippet shows you how to do this:
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; } } ... }
Once you've implemented the interface, provide the callback to Android Beam file transfer by calling {@link android.nfc.NfcAdapter#setBeamPushUrisCallback setBeamPushUrisCallback()}. The following snippet shows you how to do this:
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); ... } ... }
Note: You can also provide the array of {@link android.net.Uri} objects directly to the NFC framework through your app's {@link android.nfc.NfcAdapter} instance. Choose this approach if you can define the URIs to transfer before the NFC touch event occurs. To learn more about this approach, see {@link android.nfc.NfcAdapter#setBeamPushUris NfcAdapter.setBeamPushUris()}.
To transfer one or more files to another NFC-enabled device, get a file URI (a URI with a
file
scheme) for each file and then add the URI to an array of
{@link android.net.Uri} objects. To transfer a file, you must also have permanent read access
for the file. For example, the following snippet shows you how to get a file URI from a file
name and then add the URI to the array:
/* * 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."); }