2022-11-25 22:26阅读: 31评论: 0推荐: 0

Android通过Intent传图片

一、背景

在使用listView列表时,将图片文本存在列表中,点击列表或修改按钮时跳转到另一个活动页面显示图片。这时就需要取出图片,问题就此而来了。

二、遇到的问题

取出图片,直接对ImageView设置图片背景,不能显示。

三、解决方法

方式一(推荐):
取值:

private String[] context = {"乒乓球", "羽毛球", "网球"};
private int[] imageViews = {R.drawable.abd, R.drawable.abcd, R.drawable.absss};
private List<HashMap<String, Object>>   list = new ArrayList<>();

Intent intent = new Intent(MainActivity2.this, MainActivity3.class);
HashMap<String, Object> stringObjectHashMap = list.get(position);
ImageView imageView = view.findViewById(R.id.imageView);
//  R.drawable.add ==> stringObjectHashMap.get("image").toString()
intent.putExtra("image", stringObjectHashMap.get("image").toString());
startActivity(intent);

取值:

String photo = getIntent().getStringExtra("image").toString();
Resources resources = this.getResources();
Drawable drawable = resources.getDrawable((int) Long.parseLong(photo));
imageView.setImageDrawable(drawable);

方式二:
注意:这种方式对API有要求,
存值

 private String[] context = {"乒乓球", "羽毛球", "网球"};
 private int[] imageViews = {R.drawable.abd, R.drawable.abcd, R.drawable.absss};
 private Bitmap bitmap;
    byte buff[] = new byte[125*250];

Intent intent = new Intent(MainActivity2.this, MainActivity3.class);
HashMap<String, Object> stringObjectHashMap = list.get(position);
bitmap = BitmapFactory.decodeResource(getResources(), (Integer) stringObjectHashMap.get("image"));
 buff = Bitmap2Bytes(bitmap);
 intent.putExtra("image", buff);
 startActivity(intent);

//放在外面
private byte[] Bitmap2Bytes(Bitmap bm){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

取值:

Intent mIntent = getIntent();
byte buff[]=mIntent.getByteArrayExtra("image");
Bitmap  bitmap = BitmapFactory.decodeByteArray(buff, 0, buff.length);
BitmapDrawable mBitmapDrawable = new BitmapDrawable(bitmap);
//API16要求
imageView.setBackgroundDrawable(mBitmapDrawable);

如:
在这里插入图片描述
方式二不推荐。

posted @   所遇所思  阅读(31)  评论(0编辑  收藏  举报  
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
🔑
点击右上角即可分享
微信分享提示
  1. 1 404 not found REOL
  2. 2 偏爱 张芸京
  3. 3 Glimpse of Us Joji
偏爱 - 张芸京
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : 葛大为

作曲 : 陈伟

编曲 : 陈伟

把昨天都作废

现在你在我眼前

我想爱 请给我机会

如果我错了也承担

认定你就是答案

我不怕谁嘲笑我极端

相信自己的直觉

相信自己的直觉

顽固的人不喊累

爱上你 我不撤退

我说过 我不闪躲

我说过 我不闪躲

我非要这么做

讲不听 也偏要爱

更努力爱 让你明白

没有别条路能走

你决定要不要陪我

讲不听 偏爱

靠我感觉爱

等你的依赖

对你偏爱

痛也很愉快

把昨天都作废

把昨天都作废

现在你在我眼前

我想爱 请给我机会

如果我错了也承担

认定你就是答案

我不怕谁嘲笑我极端

相信自己的直觉

相信自己的直觉

顽固的人不喊累

爱上你 我不撤退

我说过 我不闪躲

我说过 我不闪躲

我非要这么做

讲不听 也偏要爱

更努力爱 让你明白

没有别条路能走

你决定要不要陪我

讲不听 偏爱

靠我感觉爱

等你的依赖

不后悔 有把握

不后悔 有把握

我不闪躲 我非要这么做

讲不听 也偏要爱

更努力爱 让你明白

没有别条路能走

你决定要不要陪我

讲不听 偏爱

靠我感觉爱

等你的依赖

对你偏爱 爱

痛也很愉快