intent用於不同activity間的跳轉,跳轉的同時可以附帶上參數(這有點像php中的$_GET[]和$_POST[])
有兩個相關的函數:
intent.putExtra("key", value); //寫入
value = intent.getXXXExtra("key"); //讀取,XXX是相應的數據類型,如String
對於Bitmap,由於intent不支持,則需先轉換成byte[]再傳遞。
1 private Bitmap photo_bmp = null; 2 private String nickname = null; 3 4 //Activity A: 5 private byte[] Bitmap2Bytes(Bitmap bm){ 6 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 7 bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); 8 return baos.toByteArray(); 9 } 10 11 byte buf[] = new byte[1024*1024]; 12 buf = Bitmap2Bytes(photo_bmp); 13 intent.putExtra("photo_bmp", buf); 14 intent.putExtra("nickname", nickname); 15 16 17 //Activity B: 18 byte buf[] = intent.getByteArrayExtra("photo_bmp"); 19 photo_bmp = BitmapFactory.decodeByteArray(buf, 0, buf.length); 20 nickname = intent.getStringExtra("nickname");