7

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

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

((ImageView) v).getId()

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

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

imageView.setImageResource(imgId);

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

但我得到以下异常:

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

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

任何帮助表示赞赏。

4

5 回答 5

33

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

1)首先将图像转换为字节数组,然后传递到意图,并在下一个活动中从捆绑中获取字节数组并转换为图像(位图)并设置为ImageView。

将位图转换为字节数组:-

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

将字节数组传递给意图:-

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

从 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)首先将图像保存到 SDCard 中,然后在下一个活动中将此图像设置为 ImageView。

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

于 2012-07-17T09:39:55.283 回答
6

这行不通。你必须这样尝试。

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

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

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

在您的下一个活动中,

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Bitmap");
imageView.setImageBitmap(bitmap);
于 2012-07-17T09:36:17.073 回答
0

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

public class G extends Application {
   public static Drawable imageDrawable;

   ...
}

第一项活动:

G.imageDrawable = imageView.getDrawable();

第二活动:

imgCamera.setImageDrawable(G.imageDrawable);

在 onDestroy 中:

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

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

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

</application>
于 2019-01-11T12:55:26.560 回答
0

如果您要从 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);

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

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);
    }
于 2020-01-04T16:34:40.243 回答
-1

简而言之,这是做到这一点的完美方式。这是发件人.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);

这是接收器类文件代码。

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

无需压缩。就是这样

于 2018-01-26T21:38:29.657 回答