NFC技术:使用Android Beam技术传输文件(二)

 1 public class MainActivity extends ActionBarActivity implements
 2         CreateBeamUrisCallback {
 3     private NfcAdapter mNfcAdapter;
 4     private PendingIntent mPendingIntent;
 5     private final String targetFilename = "/sdcard/temp_icon.png";
 6 
 7     @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.fragment_main);
11         mNfcAdapter = mNfcAdapter.getDefaultAdapter(this);
12         mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
13                 getClass()), 0);
14 
15         // 把资源文件的图片拷到sdcard
16         try {
17             InputStream isStream = getResources().getAssets().open("icon.png");
18             FileOutputStream fos = new FileOutputStream(targetFilename);
19             byte[] buffer = new byte[10000];
20             int n = isStream.read(buffer);
21             fos.write(buffer, 0, n);
22             isStream.close();
23 
24         } catch (Exception e) {
25             // TODO: handle exception
26         }
27         mNfcAdapter.setBeamPushUrisCallback(this, this);
28     }
29 
30     @Override
31     public Uri[] createBeamUris(NfcEvent arg0) {
32         // TODO Auto-generated method stub
33         Uri[] uris = new Uri[1];
34         Uri uri = Uri.parse("file://" + targetFilename);
35         uris[0] = uri;
36 
37         return uris;
38     }
39 
40 }
原文地址:https://www.cnblogs.com/my334420/p/6915791.html