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

8

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

我想要在Activity1中获取被点击的图片,并在Activity2中显示它。
我正在获取被点击的图片的id,像这样:

((ImageView) v).getId()

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

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

imageView.setImageResource(imgId);

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

但是我遇到了以下异常:

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

我想问题在于getId()返回的是ImageView的ID,而不是它的源图像。
所有这些图片都在drawable中。
感谢任何帮助。

看起来问题就出在这里:“我猜测这个问题是 getId() 返回的是 ImageView 的 Id 而不是它的源图片。” 你如何检索这张图片?为什么不能获取这张图片?难道不在 drawables 中吗?这些图片是来自互联网。 如果这些图片来自互联网,你可以尝试将其缓存至内存或文件中,并在下一个活动中通过放置缓存来检索它。 - Litus
那我该如何获取资源 ID ??? - GAMA
@Andro Selva 的解决方案看起来不错 :) - Litus
请查看此链接:https://dev59.com/m3I95IYBdhLWcg3w1BdN - NagarjunaReddy
@GAMA请查看我的答案,如果您有任何疑问,请告诉我。 - Dipak Keshariya
5个回答

35

解决此问题的方法有三种。

1)首先将图像转换为字节数组,然后将其传递给Intent,在下一个活动中从Bundle中获取字节数组并将其转换为图像(Bitmap),再将其设置到ImageView中。

将Bitmap转换为字节数组:

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 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) 首先将图片保存到SD卡中,然后在下一个活动中将该图片设置为ImageView。

3) 将Bitmap传递到Intent中,并在下一个活动中从Bundle中获取bitmap,但问题是如果您的Bitmap / Image大小很大,则该图像在下一个活动中无法加载。


@GAMA 如果您直接将位图传递到意图中,且该图像过大,则下一个活动中不会显示该位图。因此,传递字节数组是更安全的做法。 - Dipak Keshariya
等一下,这行代码对我来说不起作用,因为你正在给出图像的硬编码ID... Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); - GAMA
我在Activity 1中有多张图片,我想要在Activity 2中仅显示所点击的图片。因此,我不能给出硬编码的图片ID。 - GAMA
1
我通过传递图像的资源ID解决了这个问题,无论如何还是谢谢。 - GAMA
@GAMA 如果您能发布一下如何通过图像的资源ID来解决它的方法,那也会很有帮助(如果您还记得的话)。 - Basanth

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

2
我收到了 E/JavaBinder(61): !!! FAILED BINDER TRANSACTION !!!ANR in com.galley.sample (com.galley.sample/.ViewDetails) ... Reason: keyDispatchingTimedOut 的错误信息。 - GAMA

0
在您的Application类中定义一个Drawable静态变量,然后在第一个活动中设置图像可绘制数据,接着在下一个活动中从您在Application类中定义的静态变量中获取数据。
public class G extends Application {
   public static Drawable imageDrawable;

   ...
}

第一个活动:

G.imageDrawable = imageView.getDrawable();

SecondActivity:

imgCamera.setImageDrawable(G.imageDrawable);

并且在onDestroy方法中:

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

注意:您必须在清单文件中定义您的Application类:
<application
        android:name=".G"
        ...>

</application>

0
如果您正在从像适配器类之类的类移动,则使用此代码。
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);
    }

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

不需要压缩。 就是这样。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接