【问题标题】:Share Bitmap() on android to twitter, facebook, mail将 Android 上的 Bitmap() 分享到 twitter、facebook、邮件
【发布时间】:2011-04-05 16:47:07
【问题描述】:

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

我找到的代码

        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..

,我发现无法处理此问题

问候

【问题讨论】:

标签: java android bitmap android-intent share


【解决方案1】:

简单地说,您可以将位图从外部存储转换为 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);
【解决方案2】:

现在可能有点晚了,但如果您不在乎它的存储方式,您也可以使用 String url = Images.Media.insertImage(context.getContentResolver(), image, "title", null);

【讨论】:

  • 此方法为图像创建缩略图(作为文档)。
【解决方案3】:

好吧,我自己弄的,好像不把位图保存到磁盘就无法获取图片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());
}

【讨论】:

    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 1970-01-01
      • 1970-01-01
      • 2014-01-14
      • 1970-01-01
      相关资源
      最近更新 更多