从一个活动的另一个活动传递图像 [英] Passing image from one activity another activity

查看:123
本文介绍了从一个活动的另一个活动传递图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有在如此相似的问题,但没有为我工作。

There are similar questions on SO, but none worked for me.

我想获取点击的形象活动1和活性2显示它
我点击获取图像的图像ID是这样的:

I want to fetch clicked image in Activity1 and display it in Activity2.
I'm fetching image id of clicked image like this:

((ImageView) v).getId()

和通过意图传递到另一个活动。

and passing it through intent to another activity.

在第二届活动中,我使用图片ID如下:

In 2nd activity, I use image id as following:

imageView.setImageResource(imgId);

我登录这两个活动的价值OG图片ID,它是一样的。

I logged the value og image id in both the activities and it's same.

但我发现了以下异常:

android.content.res.Resources$NotFoundException: Resource is not a Drawable 
(color or path): TypedValue{t=0x12/d=0x0 a=2 r=0x7f050000}

我想这里的问题是的getId()将返回的ImageView的不是很及标识源图片
这些图像是present在绘制

I guess the problem here is getId() is returning Id of ImageView and not of it's source image.
All these images are present in drawable.

任何帮助AP preciated。

Any help appreciated.

推荐答案

有3解决方案来解决这个问题。

There are 3 Solutions to solve this issue.

1)首先将图像转换成字节数组,然后传递到意向并在接下来的活动得到捆绑字节数组转换为图像(位图),并设置成ImageView的。

1) First Convert Image into Byte Array and then pass into Intent and in next activity get byte array from Bundle and Convert into Image(Bitmap) and set into ImageView.

位图转换为字节数组: -

Convert Bitmap to Byte Array:-

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

通字节数组的意图: -

Pass byte array into intent:-

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);

获取字节数组的捆绑,并转换为位图图像: -

Get Byte Array from Bundle and Convert into Bitmap Image:-

Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);

2)第一次保存的图像到SD卡,并在接下来的活动设置此图片为ImageView的。

2) First Save image into SDCard and in next activity set this image into ImageView.

3)合格的位图到意向并得到位从包下一个活动,但问题是,如果你的位图/影像尺寸大当时的形象是不是在明年的活动加载。

3) Pass Bitmap into Intent and get bitmap in next activity from bundle, but the problem is if your Bitmap/Image size is big at that time the image is not load in next activity.

这篇关于从一个活动的另一个活动传递图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆