【问题标题】:Image only Shared with Whatsapp but not with other apps in Android图片仅与 Whatsapp 共享,但不与 Android 中的其他应用共享
【发布时间】:2016-09-09 15:58:13
【问题描述】:

我正在尝试使用意图共享图像。这是我创建的方法

public void shareImg(int fileNum)    //Consider fileNum=R.drawable.img
{

    Uri uri= Uri.parse("android.resource://"
            + context.getPackageName() + "/" + fileNum);
    Intent share=new Intent();
    share.setAction(Intent.ACTION_SEND);
    share.setType("image/*");
    share.putExtra(Intent.EXTRA_STREAM, uri);
    share.putExtra(Intent.EXTRA_TEXT, "Sent Via ---");

    Intent chooser= Intent.createChooser(share, "Share Via");
    context.startActivity(chooser);

}

图片与带有标题的 Whatsapp 正确共享。但是当我尝试与 Gmail、Messenger 等共享应用程序时,它会在 Toast 中显示错误。

例如。

Gmail 说:无法附加空文件

Messenger 说:无法转换为图片

【问题讨论】:

  • 很少有应用程序支持android.resource Uri 方案。
  • 为此,您需要先将该资源可绘制对象保存到存储中,然后与文件 URI 共享。
  • 但是当我从图库或其他应用程序中选择时它可以工作
  • @Shabbir Dhangot 但为什么它只适用于whatsapp。?
  • 正如@CommonsWare 所说,有些应用不是android.resources URI。因此,在从库中选择时,您可以传递文件,以便它们可以轻松处理。我想你明白

标签: android android-intent imageview


【解决方案1】:

您可以使用共享意图共享图像,但您必须将图像解码为本地化位图

Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "Hey view/download this image");
String path = Images.Media.insertImage(getContentResolver(), loadedImage, "", null);
Uri screenshotUri = Uri.parse(path);

intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share image via..."));

loadedImage 是图片的路径

【讨论】:

    【解决方案2】:

    这是您可以执行的步骤。

    第 1 步。首先从drawable创建位图

    Drawable d = ImagesArrayList.get(0);  
    Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
    

    第 2 步。将位图保存到文件

    FileOutputStream out = null;
    String filename = Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg";
    try {
        out = new FileOutputStream(filename);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
        // PNG is a lossless format, the compression factor (100) is ignored
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    第 3 步。与 fileurl 共享此图像文件。与共享图库图片一样共享图片。

    完整答案

    Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.xxxx); // your resource ID here
    String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/LatestShare.jpg";
    OutputStream out = null;
    File file=new File(path);
    try {
        out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    path=file.getPath();
    Uri bmpUri = Uri.parse("file://"+path);
    Intent shareIntent = new Intent();
    shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
    shareIntent.setType("image/jpg");
    startActivity(Intent.createChooser(shareIntent,"Share with"));
    

    【讨论】:

    • 保存时是否有错误。描述您在使用此解决方案时遇到的问题
    • 它给了 NullPointerException
    • 我已经尝试了您的第 3 步。但正如我所说,它没有用
    • 已经这样做了.. 实际上我犯了一个导致运行时错误的错误
    • 我是 stackoverflow 的新手,所以我需要获得 15 声望才能公开投票
    猜你喜欢
    • 1970-01-01
    • 2014-09-05
    • 1970-01-01
    • 2014-12-19
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    相关资源
    最近更新 更多