从Android应用共享图像 [英] Sharing image from android app

查看:85
本文介绍了从Android应用共享图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的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:"));
                }
            });

发生了什么事:


  1. 在WhatsApp上,我可以轻松共享图像。

  2. 在Gmail上,它说无法发送附件。

  3. 在环聊我举杯祝酒说找不到照片

  4. 在Facebook上,该帖子也没有附带图片,但我可以发布。

  5. 在Facebook Messenger上,它崩溃而无法打开。

  1. On WhatsApp I can share the image easily.
  2. On Gmail, it says that the attachment could not be sent.
  3. On Hangouts I get a toast which says Photo couldn't be found
  4. On Facebook too, the post is not accompanied with the Image but I can post.
  5. 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.

我还阅读并尝试了以下答案:问题1 问题2 ,但无济于事。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆