GAM*_*AMA 3 android android-imageview android-resources
SO上也有类似的问题,但没有一个对我有用.
我想在Activity1中获取单击的图像并在Activity2中显示它.
我正在获取点击图像的图像ID,如下所示:
((ImageView) v).getId()
Run Code Online (Sandbox Code Playgroud)
并通过意图将其传递给另一个活动.
在第二个活动中,我使用图像ID如下:
imageView.setImageResource(imgId);
Run Code Online (Sandbox Code Playgroud)
我在两个活动中记录了值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}
Run Code Online (Sandbox Code Playgroud)
我想这里的问题getId()是返回Id ImageView而不是它的源图像.
所有这些图像都存在于drawable.
任何帮助赞赏.
Dip*_*iya 25
有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();
Run Code Online (Sandbox Code Playgroud)
将字节数组传递给intent: -
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
从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);
Run Code Online (Sandbox Code Playgroud)
2)首先将图像保存到SD卡中,然后在下一个活动中将此图像设置为ImageView.
3)将Bitmap传递给Intent并从bundle中获取下一个活动中的位图,但问题是如果你的位图/图像大小很大,那时图像不会在下一个活动中加载.
这不行.你必须这样试试.
将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);
Run Code Online (Sandbox Code Playgroud)
在你的下一个活动中,
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Bitmap");
imageView.setImageBitmap(bitmap);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
50491 次 |
| 最近记录: |