打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Activity 之间传递 Bitmap 大于40M的解决方法(传递map对象的解决方法)
第一个:传递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中取出来的方法是:
[java] view plaincopy
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就可以了。再加上一个存储的代码:
[java] view plaincopy
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中:
[java] view plaincopy
Map<String,Object> data=orderlist.get(arg2-1);
SerializableMap tmpmap=new SerializableMap();
tmpmap.setMap(data);
bundle.putSerializable("orderinfo", tmpmap);
intent.putExtras(bundle);
这个SeralizableMap是自己封装的一个实现了Serializable接口的类:[java] view plaincopy
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中去,取出来的方法是:
[java] view plaincopy
Bundle bundle = getIntent().getExtras();
SerializableMap serializableMap = (SerializableMap) bundle
.get("orderinfo");
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
从相册或者相机获得uri转为bitmap
面试官:Intent 能传递多大 Size 的数据?(附阿里答案)
Android?Bundle?使用
【Android】intent传递数据时,数据大小的限制问题
Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类
Android 二维码 生成和识别(附Demo源码)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服