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 }

 

相关文章:

  • 2021-05-20
  • 2021-05-03
  • 2021-12-10
  • 2021-08-25
  • 2022-01-16
  • 2022-01-17
  • 2021-07-29
  • 2021-09-14
猜你喜欢
  • 2021-08-24
  • 2021-11-11
  • 2022-12-23
  • 2021-07-16
  • 2021-08-06
相关资源
相似解决方案