6

I have an app that takes a screenshot and the shares this with the share intent. Rather than save multiple images each time a user wants to simply share the image. Below is the code I used for images saved to the SD

Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");

dir = "file://" + Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Folder/" + location;
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(dir));

startActivity(Intent.createChooser(share, "Share Image"));

However I cant figure out how to simply save a Bitmap such as...

share.putExtra(Intent.EXTRA_STREAM, theBitmap);

Is there a way to do this without saving the image?

Thanks

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Chris
  • 1,766
  • 1
  • 21
  • 36

3 Answers3

4

What I eneded up doing was saving one "Temp" image to the SD/Phone and just overwrite the each time.

Chris
  • 1,766
  • 1
  • 21
  • 36
  • This is what I did too, but I would rather not have to use the SD card (and ask for the external storage read/write permissions). It seems like there would be a way to do this with internal storage. – Suragch May 11 '15 at 04:09
  • 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
1

Try using this one:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
Uri uri = Uri.parse("android.resource://com.your.app/drawable/" + yourimage);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(Intent.EXTRA_TEXT, shareImage);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Send your image"));

This code works fine for me to send drawables that I use in my app. No pictures saved on sd card. The only problem is that it doesn't work with some social apps :/

-1

Yes you can send the image without having to save it as a file. From just looking at the code you posted I have no idea how your sending the image but you convert the file with the following:

                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] data;
                if(yourBitmap!=null){
                yourBitmap.compress(CompressFormat.PNG, 75, bos);
                }else if (yourBitmap==null){
                    Log.w("Image going to server is null","Lost in memory");
                }
                try{
                data = bos.toByteArray();

I dont know if your sending the image to another user via your app or what but thats how I use it upload to a server. When your done with the image you would just nullify it all like this.

                bos = null;
                data = null;
                yourBitmap = null;
James andresakis
  • 5,335
  • 9
  • 53
  • 88
  • Hi, Its more an issue with share.putExtra(Intent.EXTRA_STREAM, Uri.parse(dir)) what is it suppose to be instead of the Uri? – Chris Jan 26 '12 at 17:53
  • I cant seem to get this to work, I tried share.putExtra(Intent.EXTRA_STREAM, data) however nothing attaches and data is not null. Any ideas? – Chris Jan 26 '12 at 20:47
  • How are you using the intent to send the byte array? From what I understand you cant just put things into an intent and have them be done automatically so to speak. You would normally use an intent to launch another activity or use it to launch a service. I think maybe you should look into attaching a file to an email or text through an intent because after reading your post now that Ive had some sleep it seems like you need to create an image, use reference to it in data, and attach the reference of it to the intent and then use startActivity. Im out the door at the moment so I hope that help – James andresakis Jan 26 '12 at 23:05
  • http://sree.cc/google/android/attaching-image-with-email-in-android this guys post shows how to do what Im talking about. I know you dont want to save the image but Im sure you can find a way to store the image in memory and reference it from there. – James andresakis Jan 26 '12 at 23:08
  • The link above shows how to share an image that's already been written to the SD card, so doesn't do what you're suggesting at all. – Webreaper Mar 16 '15 at 06:14