Android基础 -- Activity之间传递数据(bitmap和map对象)

原文:http://blog.csdn.net/xueerfei008/article/details/23046341

 

做项目的时候需要用到在2个activity之间传递一些数据,之前做的都是些字符串之类的东东,结果这次卡了好久,折腾了一个下午。

第一个:传递bitmap

  这个问题非常奇葩(可能我android水平还不够),居然不会报错,我是直接用bundle或Intent的extral域直接存放bitmap,结果运行时各种宕机,各种界面乱窜(我非常的纳闷)。。。搜索之后看大家都说不能直接传递大于40k的图片,然后在德问论坛上找到了解法。就是把bitmap存储为byte数组,然后再通过Intent传递。

的 

 代码如下所示:

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. Bitmap bmp=((BitmapDrawable)order_con_pic.getDrawable()).getBitmap();  
  2. Intent intent=new Intent(OrderConfirm.this,ShowWebImageActivity.class);  
  3. ByteArrayOutputStream baos=new ByteArrayOutputStream();  
  4. bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);  
  5. byte [] bitmapByte =baos.toByteArray();  
  6. intent.putExtra("bitmap", bitmapByte);  
  7. startActivity(intent);  


其中 第一行代码就是如何从一个imageview中获得其图片,这个问题也倒腾了下,貌似用setDrawingCacheEnabled也行,因为开始用的这个方法,但是直接在activity之间传递bitmap,所以导致运行时错误,后来改正之后没有再尝试。

 

先new一个ByteArrayOutputStream流,然后使用Bitmap中的compress方法,把数据压缩到一个byte中,传输就可以了。

在另一个activity中取出来的方法是:

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. imageView = (ZoomableImageView) findViewById(R.id.show_webimage_imageview);  
  2.         Intent intent=getIntent();  
  3.         if(intent !=null)  
  4.         {  
  5.             byte [] bis=intent.getByteArrayExtra("bitmap");  
  6.             Bitmap bitmap=BitmapFactory.decodeByteArray(bis, 0, bis.length);  
  7.             imageView.setImageBitmap(bitmap);  
  8.         }  

取出来字节数组之后,用BitmapFactory中的decodeByteArray方法组合成一个bitmap就可以了。

 

再加上一个存储的代码:

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. public void saveMyBitmap(String bitName,Bitmap mBitmap) throws IOException {  
  2.         File f = new File("/sdcard/Note/" + bitName);  
  3.         if(!f.exists())  
  4.             f.mkdirs();//如果没有这个文件夹的话,会报file not found错误  
  5.         f=new File("/sdcard/Note/"+bitName+".png");  
  6.         f.createNewFile();  
  7.         try {  
  8.             FileOutputStream out = new FileOutputStream(f);  
  9.              mBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);  
  10.              out.flush();  
  11.              out.close();  
  12.         } catch (FileNotFoundException e) {  
  13.                 Log.i(TAG,e.toString());  
  14.         }  
  15.          
  16.     }  



 

2.传递map对象:

封装到bundle中:

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. Map<String,Object> data=orderlist.get(arg2-1);  
  2.             SerializableMap tmpmap=new SerializableMap();  
  3.             tmpmap.setMap(data);  
  4.             bundle.putSerializable("orderinfo", tmpmap);  
  5.             intent.putExtras(bundle);  


这个SeralizableMap是自己封装的一个实现了Serializable接口的类:

 

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. public class SerializableMap implements Serializable {  
  2.     private Map<String,Object> map;  
  3.     public Map<String,Object> getMap()  
  4.     {  
  5.         return map;  
  6.     }  
  7.     public void setMap(Map<String,Object> map)  
  8.     {  
  9.         this.map=map;  
  10.     }  
  11. }  

这样才能把map对象扔到bundle中去,

 

取出来的方法是:

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
    1. Bundle bundle = getIntent().getExtras();  
    2.         SerializableMap serializableMap = (SerializableMap) bundle  
    3.                 .get("orderinfo");  
posted @ 2016-01-26 16:53  抹茶MM  阅读(660)  评论(0编辑  收藏  举报