在 Android 上将 Bitmap() 分享到 twitter、facebook、邮件

发布于 2024-10-29 20:14:52 字数 538 浏览 6 评论 0原文

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

我找到的代码

        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 的情况下处理这个问题..

问候

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

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)

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

regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱本泡沫多脆弱 2024-11-05 20:14:52

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

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

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:

return Uri.parse("file://" + imageFile.getAbsolutePath());
踏雪无痕 2024-11-05 20:14:52

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

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.

可可 2024-11-05 20:14:52

好吧,我自己得到了它,似乎没有办法在不将位图保存到磁盘的情况下获取图像 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());
}

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:

    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());
}
野心澎湃 2024-11-05 20:14:52

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

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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文