1

我正在编写一个应用程序来共享图像。我写了代码,但遇到了问题。我在做什么 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 上共享。

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

在此处输入图像描述

4

1 回答 1

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

这应该使您的图像共享。

于 2015-04-06T06:44:49.840 回答