【问题标题】:How to create a sharebutton in AndroidStudio如何在 Android Studio 中创建分享按钮
【发布时间】:2016-04-07 14:00:31
【问题描述】:

我想创建一个按钮,并在单击该按钮时共享图像。我发现我可以使用这个:

final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpg");
final File photoFile = new File(getFilesDir(), "photo.jpg");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
startActivity(Intent.createChooser(shareIntent, "Share image using")); 

但我不知道把它放在哪里(MainActivity?)我想在 WhatsApp 中分享图像。

【问题讨论】:

  • 当你点击按钮时将此代码添加到 OnClickListener 函数
  • @DanhDC 我放了 OnClickListener 但我不能让它工作 public void buttonOnClick(View v){ final Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setType("图片/jpg");最终文件 photoFile = new File(getFilesDir(), "ic_launcher.png"); shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile)); startActivity(Intent.createChooser(shareIntent, "分享图片使用")); }
  • 单击此按钮时出现任何错误。如果有错误,请显示您的 logcat。请确保您像这样实现正确的 OnClickListeneryourButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //put share action here } }) ;
  • @DanhDC 谢谢你,我成功了。我只有一个问题,我将 apk 下载到手机上,但是当我发送图像时,它说图像不在设备上。我能做什么?
  • 因为图片ic_launcher.png在你的真实设备中是不可见的,请将其更改为你手机中存在的图片。

标签: android android-studio share


【解决方案1】:
【解决方案2】:

你必须设置:shareIntent.setPackage("com.whatsapp") 才能在whatsapp中分享。

查看此帖子了解更多信息:sharing image with whatsapp in android

【讨论】:

  • 是的,但我需要将它放在 MainActivity 或按钮代码中?
  • 以onclick事件为例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
  • 1970-01-01
  • 1970-01-01
  • 2016-04-28
  • 1970-01-01
  • 1970-01-01
  • 2014-10-04
相关资源
最近更新 更多