将ImageView发送到另一个活动

时间:2013-07-04 10:30:11

标签: android

我有ImageView的活动,我希望在其他活动中发送此ImageView,我该怎么办?

4 个答案:

答案 0 :(得分:2)

您实际上无法传递ImageView本身。但是,您可以传递它的值,然后在其自己的新ImageView中重新加载它。

您可以在intent中的Activity之间传递数据。

这就是这样的例子;

Intent intent = new Intent(this, MyNewActivity.class);
intent.putExtra("EXTRA_IMAGEVIEW_URL", myImageViewData);
startActivity(intent)

然后在启动(MyNewActivity)中,您可以再次获取该数据;

String imageview_url = getIntent().getStringExtra("EXTRA_IMAGEVIEW_URL");

使用适合您的数据类型的任何方法。

编辑说明: 此解决方案假设您发送一个简单的指针指向图像,而不是图像本身。您可以从加载位置,可绘制ID或文件系统中的图像路径发送URL或URI。实际上,不要试图将整个图像本身作为base64,二进制文件或其他任何内容发送。

答案 1 :(得分:0)

你可以子类化imageview并实现可序列化的接口并以这种方式传递它,或者你可以将imageview的资源id(int)传递给另一个活动然后用这个资源id加载该活动的imageview,或者你使imageview静态然后在您只需通过FirstClass.imageView

调用它的其他活动

答案 2 :(得分:0)

您不能在活动之间传递图像视图。

假设您需要将图像从一个活动传递到另一个活动。

您可以通过将位图转换为bytearray来传递位图,如下所示

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(FirstActivity.this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);

在NextActivity中从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);

答案 3 :(得分:0)

通过将位图转换为bytearray来传递位图可以正常使用

相关问题