android从url分享图片

标签 android share

我想使用代码分享一张图片:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri imageUri = Uri.parse("http://stacktoheap.com/images/stackoverflow.png");
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(sharingIntent);

我做了一个按钮来调用上面的代码。共享 Intent 打开,但如果我单击“通过彩信共享”,我得到了:“无法将此图片添加到您的消息中”。如果 Facebook 我只有一个没有我的图片的文本区域。

最佳答案

不需要使用 ImageView 的 @eclass 答案的改编版本:

使用 Picasso将 url 加载到位图中

public void shareItem(String url) {
    Picasso.with(getApplicationContext()).load(url).into(new Target() {
        @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("image/*");
            i.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bitmap));
            startActivity(Intent.createChooser(i, "Share Image"));
        }
        @Override public void onBitmapFailed(Drawable errorDrawable) { }
        @Override public void onPrepareLoad(Drawable placeHolderDrawable) { }
    });
}

将位图转换为 Uri

public Uri getLocalBitmapUri(Bitmap bmp) {
    Uri bmpUri = null;
    try {
        File file =  new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmpUri;
}

关于android从url分享图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16300959/

相关文章:

android - 如何在 Android 中获取外部 USB 大容量存储路径?

android - 如何在谷歌播放控制台中恢复到旧版本

java - 通知监听器 - onNotificationPosted 不起作用

android - 底部对齐 float 操作按钮 - 约束布局

java - 在 android studio 中使用 LatLng 的问题

linux - 为什么我的 rpi 上的共享/in samba 不再起作用?

performance - 许多具有相同几何形状和 Material 的网格,我可以更改它们的颜色吗?

javascript - 谷歌加自定义分享按钮回调

android - 发布到 Facebook 墙时出错

image - LinkedIn API 不返回群组帖子和公司共享的 SSL 图像