【问题标题】:How to send ImageViews to another activity in android如何将 ImageViews 发送到 android 中的另一个活动
【发布时间】:2015-03-15 06:48:36
【问题描述】:

我已经在我的应用程序中实现了对图像视图的单击和双击,我想通过单击或双击将这些图像发送到另一个活动我应该怎么做?

【问题讨论】:

  • 你的图片存储在哪里
  • 只需将路径或 Uri 传递给位图作为 String 额外的 Intent 用于启动第二个 Activity
  • 我有可绘制文件中的图像
  • 这是代码@Squonk:Uri imageuri=Uri.parse("android.resource//com.example.and1/drawable/"+R.drawable.fw);意图意图=新意图(意图.ACTION_SEND); intent.setType("图片/*"); intent.putExtra(intent.EXTRA_STREAM, imageuri);

标签: android


【解决方案1】:

如果您想将图像发送到另一个活动,您实际上是在发送BitmapDrawable 对象。

首先,你可以使用一个全局变量来存储位图。他们只是将位图保存到一个活动中的变量中,然后从另一个活动中获取。

其次,你可以使用Bundle

OneActivity:

Bundle b=new Bundle();
b.putParcelable("img",yourBitmap);//put your bitmap in
Intent intent = new Intent();  
intent.setClass(OneActivity.this, AnotherActivity.class);  
intent.putExtras(b);

AnotherActivity 的 onCreate 上:

Bundle b = this.getIntent().getExtras();
Bitmap = (Bitmap)b.getParcelable("img");

希望你得到它!

【讨论】:

  • 我使用drawables。我必须将其转换为位图吗?
  • @user4672418 没有。您只需要将我的代码中的Bitmap 替换为Drawable
  • 可绘制对象本质上不是可序列化的。
  • 无论如何,在 Intent 内部交付资源并不是一个好习惯。
  • 我试过这段代码,但是当我点击按钮时,它说应用程序已经停止工作:Bundle b1=new Bundle();位图位图=BitmapFactory.decodeResource(getResources(),R.drawable.fw); b1.putParcelable("img",bitmap);//把你的位图放入Intent intent = new Intent(); int id=R.drawable.fw;意图.putExtras(b1);
【解决方案2】:

如果您的这些图像的来源在您的项目中(如可绘制文件夹),您可以将文件名字符串作为标签添加到 xml 中,并通过 EXTRA 将其添加到您的意图中。这里的小例子: https://stackoverflow.com/a/29021448/3332634

【讨论】:

  • 它不起作用。当我单击图像“应用程序已停止”时出现此错误
  • 我想要做的是单击或双击图像被发送到 enxt 活动,当我点击一个按钮时,它会转到显示图像的其他活动。
  • 但是你说你有错误。 Logcat 中的错误究竟是什么?
  • 在哪一行?复制生成它的行
  • 其实我现在正在尝试这段代码 Bundle b1=new Bundle();位图位图=BitmapFactory.decodeResource(getResources(),R.drawable.fw); b1.putParcelable("img",bitmap);//把你的位图放入Intent intent = new Intent();意图.putExtras(b1);
【解决方案3】:

希望对你有所帮助。

imageview有这个方法

 setImageResource(int resId)

所以在你的第一个活动存储中是这样的

    int yourid=R.drawable.yourimage
    Intent intent=new Intent(this,yourotheractivityname.class)
    intent.putInt("key",resId)

在你的其他活动中

    Intent intent=getIntent()
    int yourid=intent.getExtras().getInt("key")
    yourImageView.setImageResource(yourId)

【讨论】:

  • 我想要做的是,单击一下,图像就会发送到下一个活动。当我点击一个按钮时,它会转到那个活动
  • 好的,当您使用 startActivity(intent) 之类的内容进行该活动时,然后说 intent.putInt("key", id)
  • 那么在我的下一个活动中我如何得到它
猜你喜欢
  • 2014-09-06
  • 2023-03-08
  • 2016-11-04
  • 2012-07-20
  • 1970-01-01
  • 2016-08-18
  • 1970-01-01
  • 2012-08-04
相关资源
最近更新 更多