猿问

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

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

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 ImageView不是它的源图像
所有这些图像都存在于drawable

任何帮助赞赏。



森林海
浏览 487回答 3
3回答

德玛西亚99

这不行。你必须这样试试。将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);

紫衣仙女

Drawable在Application类中定义静态变量,然后在第一个活动中设置图像可绘制数据,然后在下一个活动中从您在Application类中定义的静态变量中获取数据。public&nbsp;class&nbsp;G&nbsp;extends&nbsp;Application&nbsp;{ &nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;Drawable&nbsp;imageDrawable; &nbsp;&nbsp;&nbsp;...}第一项活动:G.imageDrawable&nbsp;=&nbsp;imageView.getDrawable();SecondActivity:imgCamera.setImageDrawable(G.imageDrawable);并在onDestroy中:@Overrideprotected&nbsp;void&nbsp;onDestroy()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;G.imageDrawable&nbsp;=&nbsp;null; &nbsp;&nbsp;&nbsp;&nbsp;super.onDestroy();}注意:您必须Application在清单中定义您的类:<application &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:name=".G" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...></application>
随时随地看视频慕课网APP

相关分类

Android
我要回答