【问题标题】:Sharing image from android app从安卓应用分享图片
【发布时间】:2015-04-14 13:51:20
【问题描述】:

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

发生了什么:

  1. 我可以在 WhatsApp 上轻松分享图片。
  2. 在 Gmail 上,提示无法发送附件。
  3. 在环聊中,我得到一个祝酒词,上面写着Photo couldn't be found
  4. 在 Facebook 上也是如此,帖子没有附带图片,但我可以发布。
  5. 在 Facebook Messenger 上,它在未打开的情况下崩溃。

我为此遵循的教程给出了here。教程的send binary content 部分是我已经实现的部分。

我尝试的另一件事是将图像设置为ImageView 并查看它是否显示。图像显示正确。此外,日志消息会打印图像的正确路径。

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

我哪里错了?

【问题讨论】:

  • 使用真正的 MIME 类型。 image/* 不是真正的 MIME 类型。如果您的图像是 JPEG,请使用 image/jpeg
  • @CommonsWare 也试过了。不工作。
  • imageUrl 的实际值是多少?您在虚假代码列表中嵌入的内容不是有效的Uri。您的 Log.d() 语句实际上记录了什么?
  • @CommonsWare 好的。日志消息是:/storage/emulated/0/app_pictures/2/back.jpg 这是不正确的 Uri 吗?如果有,为什么?
  • 没有方案。它有一个重复的斜线。

标签: java android image android-sharing


【解决方案1】:

试试这个,

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();
                }

【讨论】:

    【解决方案2】:

    是的,@CommonsWare 答案是正确的,您错过了 Intent.putExtra() 中的架构,这就是它无法在其他社交媒体平台上读取您的图像的原因 这是我的解决方案

     Uri fileUri = Uri.fromFile(new File(imagePath));
    
     //No need to do mimeType work or ext 
    
     Intent intent = new Intent(Intent.ACTION_SEND);
     intent.putExtra(Intent.EXTRA_STREAM, fileUri);
     intent.setType("image/*");
     startActivity(Intent.createChooser(intent, "Share Image:"));
    

    顺便说一句,它适用于所有提到的平台

       Uri image_uri = Uri.parse("file://"+imagePath);
    

    另一种创建 Uri 并将其传递给意图的方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-03
      相关资源
      最近更新 更多