【发布时间】: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。请确保您像这样实现正确的 OnClickListener : yourButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //put share action here } }) ;
-
@DanhDC 谢谢你,我成功了。我只有一个问题,我将 apk 下载到手机上,但是当我发送图像时,它说图像不在设备上。我能做什么?
-
因为图片ic_launcher.png在你的真实设备中是不可见的,请将其更改为你手机中存在的图片。
标签: android android-studio share