简体   繁体   中英

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

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

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

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.

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());
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM