9

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

chrstnwhlrt
  • 331
  • 1
  • 4
  • 18
  • This Q&A is worth reading http://stackoverflow.com/questions/9049143/android-share-intent-for-a-bitmap-is-it-possible-not-to-save-it-prior-sharing – Suragch May 11 '15 at 04:16

4 Answers4

7

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());
feelinglucky
  • 186
  • 2
  • 5
4

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.

keyboardr
  • 841
  • 1
  • 8
  • 20
1

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());
}
chrstnwhlrt
  • 331
  • 1
  • 4
  • 18
-2

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

Monica
  • 311
  • 1
  • 8
  • 22
ingyesid
  • 2,864
  • 2
  • 23
  • 21