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

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

第一个:传递bitmap

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

代码如下所示:

  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中取出来的方法是:

  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就可以了。

再加上一个存储的代码:

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

2.传递map对象:

封装到bundle中:

  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接口的类:

  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中去,

取出来的方法是:

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

Android基础 -- Activity之间传递数据(bitmap和map对象)的更多相关文章

  1. 【Android 复习】 : Activity之间传递数据的几种方式

    在Android开发中,我们通常需要在不同的Activity之间传递数据,下面我们就来总结一下在Activity之间数据传递的几种方式. 1. 使用Intent来传递数据 Intent表示意图,很多时 ...

  2. Android 笔记-Fragment 与 Activity之间传递数据

    Fragment 与 Activity之间传递数据有两种方法.一种是使用setArgument,一种是使用接口回调.以下先学习第一种方法. (1)使用setArgument方法: 为了便于理解,我在这 ...

  3. Android中Activity之间的数据传递

    在开发中,我们经常涌用到Activity,那么既然用到了Activity,就一定免不了在两个或者多个Activity之间传递数据.这里我们先说一说原理,然后在看看代码和例子. 情况A:我们需要从Act ...

  4. [Android学习]Activity之间传递对象和对象集合

    开发过程中,Activity之间传递数据是必不可少的,android中使用Intent和Bundle作为数据载体,在Activity之间传递,对于基础数据类型,Bundle已经提供相关的put,get ...

  5. 大叔也说Xamarin~Android篇~Activity之间传递数组

    回到目录 我们在开发应用程序时,不可能只使用一个Layout或者一个Activity,比如你个管理系统,要求用户先登陆然后再使用,这时你至少要有两个activity吧,先登陆一个,然后成功后需要跳到别 ...

  6. Activity之间传递数据的方式及常见问题总结

    Activity之间传递数据一般通过以下几种方式实现: 1. 通过intent传递数据 2. 通过Application 3. 使用单例 4. 静态成员变量.(可以考虑 WeakReferences) ...

  7. Activity之间传递数据或数据包Bundle,传递对象,对象序列化,对象实现Parcelable接口

    package com.gaojinhua.android.activitymsg; import android.content.Intent; import android.os.Bundle; ...

  8. 28、activity之间传递数据&批量传递数据

    import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android ...

  9. 在activity之间传递数据

    在activity之间传递数据 一.简介 二.通过intent传递数据 1.在需要传数据的界面调用 intent.putExtra("data1", "我是fry&quo ...

随机推荐

  1. Codeforces 131C . The World is a Theatre(打表组合数)

    题目链接:http://codeforces.com/contest/131/problem/C 大意就是有n个男孩,m个女孩,从男孩中选不少于4个男孩,女孩中选不少于1个女孩,组成人数为t的队伍,问 ...

  2. openshift3.10集群部署

    简介 openshift是基于k8s的开源容器云. 要求 系统环境:CentOS 7.5 搭建一个master节点,两个node节点 注意: openshift3 依赖docker的版本为1.13.1 ...

  3. pycharm的一些快捷键[转]

    编辑类: Ctrl + Space 基本的代码完成(类.方法.属性)Ctrl + Alt + Space 类名完成Ctrl + Shift + Enter 语句完成Ctrl + P 参数信息(在方法中 ...

  4. Lenet 神经网络-实现篇(2)

    Lenet 神经网络在 Mnist 数据集上的实现,主要分为三个部分:前向传播过程(mnist_lenet5_forward.py).反向传播过程(mnist_lenet5_backword.py). ...

  5. Bugku-CTF加密篇之进制转换(二进制、八进制、十进制、十六进制,你能分的清吗?)

    进制转换 二进制.八进制.十进制.十六进制,你能分的清吗?

  6. URLEncode和URLDecode

    URLEncode.encode(String s,String utf-8) 编码 URLDEcode.decode(String %2b%,String utf-8) 解码 用法: String ...

  7. 如何让DOS命令在新窗口打开

    可以调用别外的批处理如 start a.batstart b.batstart c.bat 新建a.bat.B.BAT.C.CAT,在这几个批处理中输入你的命令. 以上我自己测试通过.

  8. eclipse 自定义代码块设置(代码模板)

    eclipse设置自定义代码模板 window -> show View -> other -> Templates 原来main模板 修改模板 再次插入

  9. PTA的Python练习题(十五)

    第4章-12 求满足条件的斐波那契数 a=eval(input()) b=c=1 d=1 for i in range(a): c=b b=d d=b+c if d>a: print('{}'. ...

  10. 吴裕雄 python 神经网络——TensorFlow 三层简单神经网络的前向传播算法

    import tensorflow as tf w1= tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1)) w2= tf.Variable( ...