1. 程式人生 > >NFC技術:使用Android Beam技術傳輸文件(二)

NFC技術:使用Android Beam技術傳輸文件(二)

imp 圖片 .com fault gen catch ret generate puts

技術分享

 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 }

NFC技術:使用Android Beam技術傳輸文件(二)