用 intent 传递 Bitmap

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");

 

posted on 2015-05-19 22:00  fashare  阅读(4130)  评论(0)    收藏  举报

导航