Every example I've read for sending a screenshot via email in Android involves saving first saving the bitmap to the SD card, and then referencing the picture later to send it in an email. Is there a way that I can send the screenshot without saving it to the phone first?
You are welcome to try creating a streaming ContentProvider
that uses a pipe, rather than a file, for the ParcelFileDescriptor
returned by openFile()
. Your provider would provide the screenshot from heap space, writing to the OutputStream
that you get from your end of the pipe. Then, you would pass a Uri
to your provider's stream in the EXTRA_STREAM
extra for the attachment. This sample app illustrates the technique, though in my case I am reading in a file to write to the OutputStream
.
This is a fair bit of work, and it may not be compatible with all email clients. For example, your pipe-based stream is not seekable, and clients might think they can seek in the stream to rewind to re-read in various bits. Also remember that you will still need to convert the Bitmap
of your screenshot into a PNG file.
If your concern is having the file out on external storage, write the screenshot to internal storage and use FileProvider
to serve it. This sample app is similar to the preceding one and illustrates the use of FileProvider
.
first saving the bitmap to the SD card
Approximately zero examples will demonstrate this, as removable storage is not readily accessible by developers. Those examples are probably demonstrating writing to external storage .
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.