将图像从一个活动发送到另一个活动?

时间:2014-02-24 05:47:08

标签: android

这太荒谬了......我正在尝试开发图像应用程序。我得到的问题是:在按钮上单击我去图库并拿起图片,我在图像视图中设置该图像具有相同的意图。现在点击按钮(如继续),我想将该图像发送到另一个具有图像视图的活动。我是android新手,我不知道。我几天都在揉头。所以请任何人帮助我摆脱这个......提前谢谢。并且还支持我让那个图像在另一个活动中全屏显示..

4 个答案:

答案 0 :(得分:1)

尝试以下代码。

要从第一项活动中拍摄图像,请尝试使用此代码

imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("BitmapImage", bitmap);

此处将imageView替换为您的imageview的ID。

在其他活动中只需编写此代码即可从第一个活动中获取图像。

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

答案 1 :(得分:1)

好的,除非你绝对必须,而且图像非常小,否则不要通过Intent发送图像。您应该已经从Gallery Intent接收图像URI。只需将该URI作为额外内容传递给您用于启动新Activity的Intent,然后使用该URI在该Activity中的ImageView上调用setImageURI()

答案 2 :(得分:1)

当您将图片从一个活动发送到另一个活动时,您应该将 位图 对象转换为 byte [] < / strong> Arary然后发送。

就像,

  ByteArrayOutputStream stream = new ByteArrayOutputStream();
  yourbitmapObjectName.compress(Bitmap.CompressFormat.PNG, 100, stream);
  byte[] byteArray = stream.toByteArray();

  Intent n = new Intent();
  n.setClass(getApplicationContext(), MainActivity.class);
  n.putExtra("picture", byteArray);
  startActivity(n);

现在只需在oncreate()方法的第二个活动中检索。

   Bundle extras = getIntent().getExtras();
   byte[] byteArray = extras.getByteArray("picture");
   Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
   if (bmp != null) {
     selected.setImageBitmap(bmp);

   }

答案 3 :(得分:0)

同意@kcoppock,不要通过意图传递图像。因为你可能会在意图传递时收到!!!Fail to deliver!!!错误。这可能是由于内存和堆限制而发生的。

所以你可以解决@kcoppock所说的问题;通过使用库中的图像URI。然后使用decodeResources的{​​{1}}并使用图片。

否则,您可以使用上一个活动中的BitmapFactoryGetter方法,然后通过Setter方法访问其他活动中的图片。