【问题标题】:Not able to share an Image on Whatsap无法在 Whatsapp 上分享图片
【发布时间】:2015-04-06 06:34:22
【问题描述】:

我正在编写一个应用程序来共享图像。 我写了代码,但遇到了问题。 我在做什么 1:将图像临时保存在内部存储中 2:传递URI分享图片。

图像已成功保存在内部存储中,但未在 whatsapp 上共享。 当我在 whatsapp 上分享它时,Whatsapp 会打开,我选择收件人,whatsapp 处理它并说“重试”。

代码:

    public void shareImage(View V)
    {

        Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.download);
        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("image/jpeg");
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
          icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

        File file =  new File(Environment.getExternalStorageDirectory()
            + File.separator + "myDownloadedImage.jpg");


     try
     {
      file.createNewFile();
      FileOutputStream fo = new FileOutputStream(file);
      fo.write(bytes.toByteArray());
      fo.close();
     } catch (IOException e) {         
      Toast.makeText(this, "Some error in Writing"+e.getMessage(), Toast.LENGTH_LONG).show();
          e.printStackTrace();
    }

   Uri downloadLocation=Uri.fromFile(file);
   share.putExtra(Intent.EXTRA_STREAM, downloadLocation);
   startActivity(Intent.createChooser(share, "Share Image"));

}

图像成功保存在内部存储中,但未在 whatsapp 上共享。

截图如下。我们可以看到图片没有成功分享。

【问题讨论】:

标签: android whatsapp android-sharing


【解决方案1】:

使用以下代码获取图片并制作 uri:

                File file =  new File(Environment.getExternalStoragePublicDirectory(  
                    Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
                file.getParentFile().mkdirs();
                FileOutputStream out = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
                out.close();
                bmpUri = Uri.fromFile(file);

现在将 bmpUri 传入:

shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);

这应该使您的图像共享。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-26
    • 2023-03-14
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多