-1

我有一个活动,ImageView我想ImageView在另一个活动中发送这个,我该怎么做?

4

4 回答 4

2

您实际上不能传递 ImageView 本身。但是,您可以传递它的值并将其重新加载到您自己的新 ImageView 中的其他 Activity 中。

您可以在 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");

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

编辑说明:此解决方案假设您发送一个指向图像的简单指针,而不是图像本身。您可以发送加载它的 URL 或 URI、可绘制 ID 或文件系统中的图像路径。事实上,不要尝试将整个图像本身作为 base64、二进制或任何你想出的东西发送。

于 2013-07-04T10:36:25.680 回答
1

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

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

您可以通过将位图转换为 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 = 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);

更新:5-9-2019 注意:最好将图像存储在磁盘上的某个位置,然后仅将图像的路径传递给下一个活动。如果图像很大,上述方法可能不起作用。

于 2013-07-04T10:39:34.597 回答
0

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

于 2013-07-04T10:33:58.503 回答
0

通过将位图转换为字节数组来传递位图对我来说很好

于 2016-03-30T14:13:11.773 回答