I'm using a library that returns a bitmap image if I want to share this image to another application or send it via email? I don't have the Uri of the image then what should I do
-
You have to store it first after that you can share it in any application. – Harshil kakadiya May 11 '19 at 10:23
2 Answers
- convert your bitmap into a file:
String path = Environment.getExternalStorageDirectory();
File file = new File(path + "/image_name.jpg");
OutputStream fOut = new FileOutputStream(file);
pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
fOut.flush(); // Not really required
fOut.close(); // do not forget to close the stream
- Get the URI from the file:
Uri yourUri = Uri.fromFile(file);
Addition: for android 7.0+ you need a different way to get URI from file.
Saving file to the default main directory is only an example but maybe not the best practice.
-
This will not work on Android 7.0+, as you cannot share a `file` `Uri`. Also, putting things in the root of external storage is very inappropriate -- this is why Android Q/R are basically eliminating your solution. – CommonsWare May 11 '19 at 11:03
-
It all depends on your environment: - you can get hand over the complete image (e.g. an attachment in an email) - if you have an solution which could be accessed from the target, then you can create your own uri. On a local system, you could store the image in a file. Or if you have a web application, you might want to make the image available so that you can provide an url to the image, too)
So it mainly depends on the whole Szenario: where does the image come from, how big is it? What is the target? ....
And sorry, that I do Not write this as a comment - need a reputation of 50 to write comments first ....

- 736
- 3
- 7
-
-
Ohh, right. Missed that completely. Thank you. Will have a closer look at all tags in the future. – Konrad Neitzel May 11 '19 at 11:10