> Android allows you to transfer large files between devices using the Android Beam file transfer feature.This feature has a simple API and allows users to start the transfer process by simply touching devices. In response, Android Beam file transfer automatically copies files from one device to the other and notifies the user when it's finished.
While the Android Beam file transfer API handles large amounts of data, the Android Beam NDEF transfer API introduced in Android 4.0 (API level 14) handles small amounts of data such as URIs or other small messages. In addition, Android Beam is only one of the features available in the Android NFC framework, which allows you to read NDEF messages from NFC tags.
> 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:
- Android Beam file transfer for large files is only available in Android 4.1 (API level 16) and higher.
- Files you want to transfer must reside in external storage. To learn more about using external storage, read Using the External Storage.
- Each file you want to transfer must be world-readable. You can set this permission by calling the method
File.setReadable(true,false)
. - You must provide a file URI for the files you want to transfer. Android Beam file transfer is unable to handle content URIs generated by
FileProvider.getUriForFile
.
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />Note: As of Android 4.2.2 (API level 17), this read 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.
> 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); ... } } ... }
>Specify the Files to Send
/* * 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."); }> In the
<intent-filter>
element, add the following child elements:
<action android:name="android.intent.action.VIEW" />
ACTION_VIEW
intent sent from the notification.
<category android:name="android.intent.category.CATEGORY_DEFAULT" />
Intent
that doesn't have an explicit category.
<data android:mimeType="mime-type" />
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(); }
To determine if you can retrieve a file directory from the content URI, determine the the content provider associated with the URI by calling Uri.getAuthority()
to get the URI's authority. The result has two possible values:
-
The URI is for a file or files tracked by
MediaStore
. Retrieve the full file name fromMediaStore
, and get directory from the file name.
Any other authority value
- A content URI from another content provider. Display the data associated with the content URI, but don't get the file directory.
MediaStore.AUTHORITY