Develop -- Training(十一) -- 通过NFC共享文件
2016-03-06 23:29
519 查看
Android Beam 进场通信,通过 NFC 来分享照片、视频、蓝牙配对,进行一些通信操作。
Android Beam 只有在 Android 版本40.以上,API等级14。
1.Android Beam 传输大文件只能在 Android 4.1(API级别16)和更高可用。
2.文件传输必须要在外部存储。
3.每个文件必须是全部范围可读。可以通过调用方法设置此权限File.setReadable(true ,false)。
4.传输的文件的必须要有文件URI。Android Beam的文件传输无法处理通过 FileProvider.getUriForFile 所产生的内容的URI。
检测是否支持 Android Beam 文件传输
创建提供共享文件的回调方法
通过 Activity 来接收数据处理。
读取 Android Beam 文件传输是需要权限的
处理接收相应的Uri
Android Beam 只有在 Android 版本40.以上,API等级14。
将文件发送到另一台设备
Android Beam 文件传输的要求:1.Android Beam 传输大文件只能在 Android 4.1(API级别16)和更高可用。
2.文件传输必须要在外部存储。
3.每个文件必须是全部范围可读。可以通过调用方法设置此权限File.setReadable(true ,false)。
4.传输的文件的必须要有文件URI。Android Beam的文件传输无法处理通过 FileProvider.getUriForFile 所产生的内容的URI。
<!--权限申明--> <uses-permission android:name="android.permission.NFC" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!--使用NFC功能--> <uses-feature android:name="android.hardware.nfc" android:required="true" /> <!--最低要设置为16以上--> android:minSdkVersion="16"
检测是否支持 Android Beam 文件传输
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); ... } } ... }
创建提供共享文件的回调方法
public class MainActivity extends Activity { // List of URIs to provide to Android Beam private Uri[] mFileUris = new Uri[10]; // Instance that returns available files from this app private FileUriCallback mFileUriCallback; 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); /** * 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; } } }
从另一个设备接收文件
Android Beam的 文件传输将文件复制到接收设备上的一个特殊的目录。响应请求显示数据。通过 Activity 来接收数据处理。
<action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.CATEGORY_DEFAULT" /> <data android:mimeType="mime-type" />
读取 Android Beam 文件传输是需要权限的
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
处理接收相应的Uri
public class MainActivity extends Activity { ... // A File object containing the path to the transferred files private File mParentPath; // Incoming Intent private Intent mIntent; ... /* * 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 mIntent = getIntent(); String action = mIntent.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 = mIntent.getData(); /* * Test for the type of URI, by getting its scheme value */ if (TextUtils.equals(beamUri.getScheme(), "file")) { mParentPath = handleFileUri(beamUri); } else if (TextUtils.equals( beamUri.getScheme(), "content")) { mParentPath = handleContentUri(beamUri); } } ... } // 处理文件的Uri 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(); } // 处理内容的Uri 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 new File(copiedFile.getParent()); } else { // The query didn't work; return null return null; } } } }
相关文章推荐
- How the Main Thread works
- 严重: Failed to initialize end point associated with ProtocolHandler ["http-bio-8080"]
- FZU 1001 Duplicate Pair(简单题目)
- 例题6-22 UVA 11853 Paintball 战场
- web service中chain的使用
- 软件测试(二)——分析程序中的错误(Fault,Failure,Error)
- keyboard iPhone-Portrait-NumberPad; using 563160167_Portrait_iPhone-Simple-Pad_Default
- HUST 1606 Naive
- Contains Duplicate
- AIX Study之--AIX网卡配置管理(ent0、en0、et0)
- shrio 权限管理filterChainDefinitions过滤器配置
- POJ 2010 Moo University - Financial Aid (优先队列/二分答案)
- BaiduMap API 使用
- Develop -- Training(十) -- 共享文件
- Failed to extend swap file from 0 kb to xxx kb.
- 理解int main(int argc,char **argv)中的参数的意义
- 进程等待示例(wait与waitpid调用)
- 源码阅读笔记 – 2 std::vector (2) 关于Allocator Aware Container特性
- HDU 1104 Remainder
- 笔记:batch normalization:accelerating deep network training by reducing internal covariate shift