【问题标题】:Passing image from one activity another activity从一个活动传递图像另一个活动
【发布时间】:2012-07-16 05:00:32
【问题描述】:

关于 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() 返回的是 ImageView 的 ID,不是它的源图像
所有这些图片都存在于drawable

任何帮助表示赞赏。

【问题讨论】:

  • 确实是这个问题:“我猜这里的问题是 getId() 正在返回 ImageView 的 ID,而不是它的源图像。”您如何检索此图像?为什么你不能得到图像?不在drawables中?这些图片来自互联网。如果图像来自互联网,您可以尝试将图像缓存在内存或文件中,并通过将缓存放在下一个活动中检索它
  • 那么我如何获得 RESOURCE ID ???
  • @Andro Selva 解决方案似乎不错:)
  • @GAMA 请看我的回答,如果您有任何疑问,请告诉我。

标签: android android-imageview android-resources


【解决方案1】:

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

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

【讨论】:

  • @GAMA 如果您直接将位图传递给意图,并且当时图像很大,则位图不会显示在下一个活动中,因此传递字节数组是安全的。
  • 等一下,这条线对我不起作用,因为你提供了图像的硬编码 id...Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
  • 我在活动 1 中有多个图像,我只想在活动 2 中显示点击的图像。所以我不能给出硬编码的图像ID。
  • 我通过传递图像的资源 ID 解决了这个问题,无论如何都是 Thnx。
  • @GAMA 如果您可以通过传递图像的资源 ID 来发布您如何解决它,它也会有所帮助(如果您记得的话)
【解决方案2】:

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

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

在你的下一个活动中,

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

【讨论】:

  • 我收到 E/JavaBinder(61): !!! FAILED BINDER TRANSACTION !!!ANR in com.galley.sample (com.galley.sample/.ViewDetails) ... Reason: keyDispatchingTimedOut
【解决方案3】:

Application 类中定义一个Drawable 静态变量,然后在第一个活动中设置图像可绘制数据,然后在下一个活动中从您在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>

【讨论】:

    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      简而言之,这是实现此目的的完美方式。 这是发件人.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);
      

      无需压缩。 就是这样

      【讨论】:

        猜你喜欢
        • 2012-03-05
        • 1970-01-01
        • 1970-01-01
        • 2018-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多