简体   繁体   English

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

[英]Passing image from one activity another activity

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.我想在 Activity1 中获取点击的图像并将其显示在 Activity2 中。
I'm fetching image id of clicked image like this:我正在像这样获取点击图像的图像 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 image 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 ImageView and not of it's source image .我想这里的问题是getId()返回的是ImageView的 Id 而不是它的源图像
All these images are present in drawable .所有这些图像都存在于drawable中。

Any help appreciated.任何帮助表示赞赏。

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)首先将图像转换为字节数组,然后传递给 Intent,在下一个活动中,从 Bundle 中获取字节数组并转换为图像(位图)并设置到 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) 将位图传递给 Intent 并在下一个活动中从捆绑包中获取位图,但问题是如果您的位图/图像大小当时很大,则图像不会在下一个活动中加载。

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);

Define a Drawable static variable in your Application class, 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 Application class.在您的Application类中定义一个Drawable静态变量,然后在第一个活动中设置图像可绘制数据,然后在您的下一个活动中从您在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 Application class in the manifest:注意:您必须在清单中定义您的Application类:

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

</application>

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);
    }

The perfect way to do this in short.简而言之,这是执行此操作的完美方法。 This is the code of sender.class file这是sender.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而已

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM