简体   繁体   English

仅与Whatsapp共享的图像,但不与Android中的其他应用共享

[英]Image only Shared with Whatsapp but not with other apps in Android

I am trying to share image using intent. 我正在尝试使用意图共享图像。 Here is the method that i created 这是我创建的方法

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

}

The image is shared properly with Whatsapp with caption. 图像可以与带字幕的Whatsapp正确共享。 But when I try to share app with Gmail, Messenger, etc it gives error shown in Toast. 但是,当我尝试与Gmail,Messenger等共享应用程序时,会出现Toast中显示的错误。

For eg. 例如。

Gmail says : Can't attach empty file Gmail说:无法附加空文件

Messenger says : Failed to convert to image Messenger说:无法转换为图像

You can share image using share intent, but you've to decode image to a localized Bitmap 您可以使用共享意图共享图像,但是必须将图像解码为本地化的位图

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 is the Path of image loadImage是图像的路径

Here is the steps you can perform. 这是您可以执行的步骤。

Step 1 . 步骤1 First create bitmap from drawable 首先从drawable创建位图

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

Step 2 . 第二步 Save Bitmap to File 将位图保存到文件

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

Step 3 . 第三步 Share this image File with fileurl. 使用fileurl共享此图像文件。 Share the Image same as you sharing gallery image. 共享图像与共享图库图像相同。

Complete Answer 完整答案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM