java 将 Android 上的 Bitmap() 分享到 twitter、facebook、mail

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5555405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:38:53  来源:igfitidea点击:

Share Bitmap() on android to twitter, facebook, mail

javaandroidbitmapandroid-intentshare

提问by chrstnwhlrt

maybe an easy question: I want to share a bitmap I received over the net to twitter/facebook/etc with the default share "intent".

也许是一个简单的问题:我想将我通过网络收到的位图与默认共享“意图”共享到 twitter/facebook/etc。

The code I found

我找到的代码

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

needs to be filled at the point "IDONTKNOW" with the bitmap. (this.bitmap)

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

I found no way to handle this without saving the bitmap to internal sd..

我发现没有将位图保存到内部 sd 的方法来处理这个问题。

regards

问候

采纳答案by feelinglucky

Simply, you can convert a bitmap into PNG from external storage.

简单地说,您可以将位图从外部存储转换为 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();

Then, you can get a URI through Uri.parse:

然后,你可以通过 Uri.parse 得到一个 URI:

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

回答by keyboardr

Might be a little late now, but you could also do String url = Images.Media.insertImage(context.getContentResolver(), image, "title", null);if you don't care how it's stored.

现在可能有点晚了,但String url = Images.Media.insertImage(context.getContentResolver(), image, "title", null);如果你不关心它是如何存储的,你也可以这样做。

回答by chrstnwhlrt

Ok I got it on my own, it seems there is no way to get the image uri without saving the bitmap to disk, therefore I use this simple method:

好的,我自己得到了它,如果不将位图保存到磁盘,似乎无法获取图像 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());
}

回答by ingyesid

Send Binary Content Binary data is shared using the ACTION_SEND action combined with setting the appropriate MIME type and placing the URI to the data in an extra named EXTRA_STREAM. This is commonly used to share an image but can be used to share any type of binary content:

发送二进制内容 二进制数据使用 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)));

source http://developer.android.com/training/sharing/send.html

http://developer.android.com/training/sharing/send.html