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

这边文章很古老了,看到还有朋友回复,这里更新几点,避免误导大家。

第一,传递HashMap对象

 HashMap本身已经实现了Cloneable, Serializable,Intent传递时,直接强转就可以了。(感谢@qcks指正)

Map<String, String> params = new HashMap<>();
params.put("key", "value");

Intent intent = new Intent(app, Activity.class);
intent.putExtra("parmas",(Serializable)params);

2. 传递Bitmap

强调一遍一定不要通过Intent传

2.1 本地资源只传递R.id,然后通过resource去解析出来;

2.2 如果是SDCard中的文件,只传递Uri;

2.3 如果是网络流,先本地保存图片,然后再传递路径Uri。

 

 另,Intent可传递的数据大小有限,具体多少我并未实际测试过,跟ROOM设置的APP最大内存也有关系,网上有人做了实际代码测试,地址:https://blog.csdn.net/wingichoy/article/details/50679322

 

当时的写法水平很low,只是为了完成项目,弄了一个能用的方案,回头再来看是在是水的一比。

 

 

 

 

不用再往下看了,以前的古老方案实在是不堪入目~~~

--------------------------------------------------我是分割线 2018.8.20,----------------------------------------------

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

第一个:传递bitmap

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

的 

 代码如下所示:

 

	Bitmap bmp=((BitmapDrawable)order_con_pic.getDrawable()).getBitmap();
	Intent intent=new Intent(OrderConfirm.this,ShowWebImageActivity.class);
	ByteArrayOutputStream baos=new ByteArrayOutputStream();
	bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
	byte [] bitmapByte =baos.toByteArray();
	intent.putExtra("bitmap", bitmapByte);
	startActivity(intent);


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

 

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

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

 

imageView = (ZoomableImageView) findViewById(R.id.show_webimage_imageview);
		Intent intent=getIntent();
		if(intent !=null)
		{
			byte [] bis=intent.getByteArrayExtra("bitmap");
			Bitmap bitmap=BitmapFactory.decodeByteArray(bis, 0, bis.length);
			imageView.setImageBitmap(bitmap);
		}

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

 

再加上一个存储的代码:

 

public void saveMyBitmap(String bitName,Bitmap mBitmap) throws IOException {
	    File f = new File("/sdcard/Note/" + bitName);
		if(!f.exists())
			f.mkdirs();//如果没有这个文件夹的话,会报file not found错误
		f=new File("/sdcard/Note/"+bitName+".png");
	    f.createNewFile();
	    try {
	    	FileOutputStream out = new FileOutputStream(f);
	    	 mBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
	    	 out.flush();
	    	 out.close();
	    } catch (FileNotFoundException e) {
	            Log.i(TAG,e.toString());
	    }
	   
	}

 

 

 

 

 

2.传递map对象:

封装到bundle中:

 

	Map<String,Object> data=orderlist.get(arg2-1);
				SerializableMap tmpmap=new SerializableMap();
				tmpmap.setMap(data);
				bundle.putSerializable("orderinfo", tmpmap);
			    intent.putExtras(bundle);


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

 

 

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;
	}
}

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

 

取出来的方法是:

 

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

 

 

 

 

 

 

  • 15
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值