Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a memory mapped InputStream from a content provider?

The the client side of a content provider consumer I can do something like this, to get a proper InputStream for reading the picture:

InputStream is = getContentResolver().openInputStream(pictureUri);

It is a nice API, and will on the server side, the actual content provider result in a call to:

public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
  // Open a proper ParcelFileDescriptor, most likely using openFileHelper(uri, mode)
}

But what if the picture mapped to the URI is not to be found on the filesystem, but as a memory resource, or generated on the fly.

Can I create a memory mapped File or InputStream, or anything else, so that I am not required to save a temporary file to disk, just to be able to return it to my content provider consumer?

like image 808
PeyloW Avatar asked Oct 09 '09 05:10

PeyloW


2 Answers

This is tough. You might be able to get away with using anything that can use a Socket interface. I haven't done it, but this is what makes me think so:

http://developer.android.com/reference/android/os/ParcelFileDescriptor.html#fromSocket(java.net.Socket)

And a Socket could, in theory, be an Internet resource, or most anything...if you're willing to work at the Socket level. I would probably just give up and create the temporary file. Perhaps that makes me a coward.

like image 192
Eric Mill Avatar answered Oct 20 '22 00:10

Eric Mill


As of android-9, you can make a pipe using ParcelFileDescriptor. You can stuff up to 64k in it and be done with it, or you can set up another thread to fill it after its been read. See the answer here for more detail:

Custom ContentProvider - openInputStream(), openOutputStream()

like image 44
Hans-Christoph Steiner Avatar answered Oct 19 '22 23:10

Hans-Christoph Steiner