我正在尝试分享我的 Android 应用程序中的图像。我正在尝试将其作为电子邮件附件以及 WhatsApp 上的照片发送。
代码是:
String imageUrl = Path to image (eg. sdcard/pictures/image1.jpg);
shareImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri uriToImage= Uri.parse(imageUrl);
Log.d("Image", ""+uriToImage);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share image:"));
}
});
发生的事情是:
- 在 WhatsApp 上我可以轻松分享图像。
- Gmail 上显示附件无法发送。
- 在环聊中,我收到的祝酒词上写着
Photo couldn't be found
- 在 Facebook 上,该帖子也没有附带图片,但我可以发布。
- 在 Facebook Messenger 上,它无法打开就崩溃了。
给出了我为此遵循的教程here http://developer.android.com/training/sharing/send.html. The send binary content
本教程的一部分是我已经实现的。
我尝试的另一件事是将图像设置为ImageView
并查看是否显示。图像显示正确。此外,日志消息打印图像的正确路径。
我还阅读并尝试了以下问题的答案:问题1 https://stackoverflow.com/questions/10795630/share-image-from-sd-card and 问题2 https://stackoverflow.com/questions/9018363/android-share-bitmap-not-saved-to-sd但无济于事。
我哪里错了?
尝试这个,
try
{
File myFile = new File(share_image_path);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = myFile.getName().substring(myFile.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(ext);
Intent sharingIntent = new Intent("android.intent.action.SEND");
sharingIntent.setType(type);
sharingIntent.putExtra("android.intent.extra.STREAM", Uri.fromFile(myFile));
startActivity(Intent.createChooser(sharingIntent, "Share using"));
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)