用Bundle传Bitmap图片

方法一:

 在做项目中要实现将图片传到另外一个Activity中,利用利用bunble传值 

1、Bitmap bitmap = null;Intent intent = new Intent();Bundle bundle = new Bundle();bundle.putParcelable("bitmap", bitmap);intent.putExtras( bundle);

复制代码


bitmap已经自己实现了Parcelable接口 接收:

 2、Bundle bundle = getIntent.getExtras();Bitmap bitmap = bundle.getParcelable("bitmap");bitmap.setImageBitmap(bitmap);

 

这种做法,连接真机测试LogCat中出现!!!FAILED BINDER TRANSACTION!!!

通过google得知,原来图片的size不能超过40k。 
Activity中ImageView是不会出现这个问题, 
但是Widget使用的是remoteViews。 Intent传输的bytes不能超过40k。

 

 

方法二、

android开发默认情况下,通过Bundle bundle=new Bundle();传递值是不能直接传递map对象的,解决办法:

第一步:封装自己的map,实现序列化即可

复制代码
/**
 * 序列化map供Bundle传递map使用
 * Created  on 13-12-9.
 */
public class SerializableMap implements Serializable {
 
    private Map<String,Object> map;
 
    public Map<String, Object> getMap() {
        return map;
    }
 
    public void setMap(Map<String, Object> map) {
        this.map = map;
    }
}
复制代码

第二步:传递数据:

复制代码
Intent intent=new Intent(ListViewActivity.this,UpdateWatchActivity.class);
                                //传递数据
                                final SerializableMap myMap=new SerializableMap();
                                myMap.setMap(map);//将map数据添加到封装的myMap<span></span>中
                                Bundle bundle=new Bundle();
                                bundle.putSerializable("map", myMap);
                                intent.putExtras(bundle);
复制代码

 

第三步:接收数据:

Bundle bundle = getIntent().getExtras();
SerializableMap serializableMap = (SerializableMap) bundle.get("map");

 

这种方法Android java.lang.RuntimeException:Parcelable encountered IOException writing serializable objec

Activity之间传递数据时,为了方便,将很多数据封装成对象,然后将整个对象传过去。传对象有2中情况,

一种是实现Parcelable接口的

一种是实现Serializable接口的

用bundle.putSerializable(key ,object)产地参数或者用intent.putExtra(key object)传递参数。

但是使用时候,出现了异常

1.java.io.NotSerializableException异常

原因:对象没有实现Serializable接口

解决办法:让对象去实现Serializable接口

2.java.lang.RuntimeException异常

 

原因:对象没有实现Parcelable接口

解决办法:让对象去实现Parcelable接口

 

我初步试了,解决不了.

 

 

方法三、

把bitmap存储为byte数组,然后再通过Intent传递。

的 

 代码如下所示:

 

[java] view plain copy
 
 在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.PNG100, baos); 
  5. //图片格式很重要,可能为jpeg等,不然出现异常
  6. byte [] bitmapByte =baos.toByteArray();  
  7. intent.putExtra("bitmap", bitmapByte);  
  8. startActivity(intent);  


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

 

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

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

 

[java] view plain copy
 
 在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 plain copy
 
 在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对象:

 

 

 

 

 




posted @ 2016-03-17 12:25  代码缔造的帝国  阅读(2070)  评论(0编辑  收藏  举报