繁体   English   中英

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

[英]Passing image from one activity another activity

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中。

任何帮助表示赞赏。

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

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

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

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

在您的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>

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

简而言之,这是执行此操作的完美方法。 这是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);

这是接收器类文件代码。

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

无需压缩。 而已

暂无
暂无

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

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