android-sharing files with NFC

> 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:

  1. Android Beam file transfer for large files is only available in Android 4.1 (API level 16) and higher.
  2. Files you want to transfer must reside in external storage. To learn more about using external storage, read Using the External Storage.
  3. Each file you want to transfer must be world-readable. You can set this permission by calling the methodFile.setReadable(true,false).
  4. 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" />
Matches the  ACTION_VIEW intent sent from the notification.
<category android:name="android.intent.category.CATEGORY_DEFAULT" />
Matches an  Intent that doesn't have an explicit category.
<data android:mimeType="mime-type" />
Matches a MIME type. Specify only those MIME types that your app can handle.

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:

MediaStore.AUTHORITY
The URI is for a file or files tracked by  MediaStore. Retrieve the full file name from  MediaStore, 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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值