从Android应用共享图像 [英] Sharing image from android app
问题描述
我正在尝试从我的Android应用共享图像。我正在尝试将其作为电子邮件附件以及在WhatsApp上的照片发送。
I am trying to share an image from my Android app. I am trying to send it as an Email attachment as well as a photo on 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上,它说无法发送附件。
- 在环聊我举杯祝酒说
找不到照片
- 在Facebook上,该帖子也没有附带图片,但我可以发布。
- 在Facebook Messenger上,它崩溃而无法打开。
- On WhatsApp I can share the image easily.
- On Gmail, it says that the attachment could not be sent.
- On Hangouts I get a toast which says
Photo couldn't be found
- On Facebook too, the post is not accompanied with the Image but I can post.
- On Facebook Messenger, it crashes without opening.
该教程我已经在此处给出了此操作。本教程的发送二进制内容
部分是我已经实现的部分。
The tutorial that I have followed for this is given here. The send binary content
part of the tutorial is the one that I have implemented.
我尝试的另一件事是在 ImageView
中设置图像,然后查看是否显示。图像显示正确。另外,日志消息还会打印图像的正确路径。
Another thing I tried was to set the image in an ImageView
and see if it is displayed. The image is displayed correctly. Also, the log message prints the correct path of the image.
I also read and tried the answers to: Question 1 and Question 2 but to no avail.
我要去哪里错了?
推荐答案
尝试一下,
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();
}
这篇关于从Android应用共享图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!