Android 将图像从一个活动传递到另一个活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11519691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:54:26  来源:igfitidea点击:

Passing image from one activity another activity

androidandroid-imageviewandroid-resources

提问by GAMA

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

SO上有类似的问题,但没有一个对我有用。

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

我想在 Activity1 中获取单击的图像并将其显示在 Activity2 中。
我正在获取单击图像的图像 ID,如下所示:

((ImageView) v).getId()

and passing it through intent to another activity.

并将其通过意图传递给另一个活动。

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

在第二个活动中,我使用图像 ID 如下:

imageView.setImageResource(imgId);

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

我在两个活动中都记录了值 og 图像 ID,它是相同的。

But I'm getting following exception:

但我收到以下异常:

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

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

我想这里的问题getId()是返回 Id ofImageView不是它的源图像
所有这些图像都存在于drawable.

Any help appreciated.

任何帮助表示赞赏。

回答by Dipak Keshariya

There are 3 Solutions to solve this issue.

有 3 种解决方案可以解决此问题。

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.

1)首先将图像转换为字节数组,然后传递给意图,在下一个活动中从捆绑包中获取字节数组并转换为图像(位图)并设置为 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 中获取字节数组并转换为位图图像:-

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) First Save image into SDCard and in next activity set this image into ImageView.

2) 首先将图像保存到 SDCard 中,然后在下一个活动中将此图像设置为 ImageView。

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.

3) 将 Bitmap 传递到 Intent 并从 bundle 中获取下一个活动中的位图,但问题是如果您的 Bitmap/Image 大小当时很大,则图像不会在下一个活动中加载。

回答by Andro Selva

This won't work. You have to try it this way.

这行不通。你必须以这种方式尝试。

Set the DrawingCache of your ImageView to be true and then save the background as a Bitmap and pass it via putExtra.

将 ImageView 的 DrawingCache 设置为 true,然后将背景保存为 Bitmap 并通过 putExtra 传递它。

image.setDrawingCacheEnabled(true);
Bitmap b=image.getDrawingCache();
Intent i = new Intent(this, nextActivity.class);

i.putExtra("Bitmap", b);
startActivity(i);

And in your Next Activity,

在你的下一个活动中,

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Bitmap");
imageView.setImageBitmap(bitmap);

回答by Nobody8

Define a Drawablestatic variable in your Applicationclass, and then set image drawable data in the first activity, then in your next activity get data from the static variable you defied in your Applicationclass.

Drawable在你的Application类中定义一个静态变量,然后在第一个活动中设置图像可绘制数据,然后在你的下一个活动中从你在Application类中定义的静态变量中获取数据。

public class G extends Application {
   public static Drawable imageDrawable;

   ...
}

First activity:

第一个活动:

G.imageDrawable = imageView.getDrawable();

SecondActivity:

第二个活动:

imgCamera.setImageDrawable(G.imageDrawable);

and in onDestroy:

在 onDestroy 中:

@Override
protected void onDestroy() {
    G.imageDrawable = null;
    super.onDestroy();
}

Note: You have to define your Applicationclass in the manifest:

注意:您必须Application在清单中定义您的类:

<application
        android:name=".G"
        ...>

</application>

回答by Afzaal Ahmed

If you are moving from class like addapter class then use this code.

如果您从类似 addapter 类的类中移动,请使用此代码。

Bitmap bitImg=listItem.getBitmapImage();
    ByteArrayOutputStream baoS = new ByteArrayOutputStream();
    bitImg.compress(Bitmap.CompressFormat.JPEG, 50, baoS);
    intent.putExtra("bitArray", baoS.toByteArray());
    context.getApplicationContext().startActivity(intent);

and then past this to next activity

然后将其传递到下一个活动

if(getIntent().hasExtra("bitArray")) {                
Bitmap bitM = BitmapFactory.decodeByteArray( getIntent().getByteArrayExtra("bitArray"),0,getIntent().getByteArrayExtra("bitArray").length);       
        imgIT = findViewById(R.id.img_detail);
        imgIT.setImageBitmap(bitM);
    }

回答by Abdul Qadir

The perfect way to do this in short. This is the code of sender .class file

简而言之,这是做到这一点的完美方式。这是发件人 .class 文件的代码

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher;
Intent intent = new Intent();
Intent.setClass(<Sender_Activity>.this, <Receiver_Activity.class);
Intent.putExtra("Bitmap", bitmap);
startActivity(intent);

and this is receiver class file code.

这是接收器类文件代码。

Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");
ImageView viewBitmap = (ImageView)findViewById(R.id.bitmapview);
viewBitmap.setImageBitmap(bitmap);

No need to compress. that's it

无需压缩。就是这样