在安卓上分享Bitmap()到Twitter、Facebook和邮件

9
也许这是一个简单的问题:我想通过默认的分享“意图”将我从网络上接收到的位图分享到twitter/facebook等社交媒体平台。
我找到的代码:
        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        sendIntent.setType("image/jpeg");
        sendIntent.putExtra(Intent.EXTRA_STREAM, "IDONTKNOW");
        sendIntent.putExtra(Intent.EXTRA_TEXT,
                "See my captured picture - wow :)");
        startActivity(Intent.createChooser(sendIntent, "share"));

需要在“IDONTKNOW”点用位图(this.bitmap)填充。

我找不到不保存位图到内部SD卡的处理方式。

祝好。


这个Q&A非常值得一读:https://dev59.com/sGox5IYBdhLWcg3wq2EC - Suragch
4个回答

7
简单来说,你可以将外部存储中的位图转换为PNG格式。
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File imageFile = new File(path, getCurrentTime()+ ".png");
FileOutputStream fileOutPutStream = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 80, fileOutPutStream);

fileOutPutStream.flush();
fileOutPutStream.close();

然后,您可以通过Uri.parse获取URI:

return Uri.parse("file://" + imageFile.getAbsolutePath());

字符串路径 = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); - Vivek Av

4

可能有点晚了,但是如果您不关心它是如何存储的,您也可以执行 String url = Images.Media.insertImage(context.getContentResolver(), image, "title", null);


此方法创建图像的缩略图(作为文档)。 - Muzaffer

1

好的,我自己解决了,似乎没有办法在不将位图保存到磁盘的情况下获取图像URI,因此我使用了这个简单的方法:

    private Uri storeImage() {
    this.storedImage = null;
    this.storeImage = true;
    // Wait for the image
    while (this.storedImage == null && !this.stop)
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    this.storeImage = false;
    FileOutputStream fileOutputStream = null;
    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File file = new File(path, "cwth_" + getCurrentTime()+ ".jpg");
    try {
        fileOutputStream = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
    this.storedImage.compress(CompressFormat.JPEG, JPEG_STORE_QUALITY, bos);
    try {
        bos.flush();
        bos.close();
        fileOutputStream.flush();
        fileOutputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Uri.parse("file://" + file.getAbsolutePath());
}

-2

发送二进制内容 使用ACTION_SEND操作共享二进制数据,结合设置适当的MIME类型并将数据的URI放置在名为EXTRA_STREAM的额外参数中。这通常用于共享图像,但也可用于共享任何类型的二进制内容:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));

源代码 http://developer.android.com/training/sharing/send.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接