Question

what is the correct way how I should form the intent to show content from my app in 3rd party viewers? I need to show images in gallery (or any other image viewer), pdfs in some pdf reader,..

Data gets server through a content provider which implements the openFile() method and returns a output pipe..

ParcelFileDescriptor[] pipe=ParcelFileDescriptor.createPipe();
...
ParcelFileDescriptor.AutoCloseOutputStream stream = new ParcelFileDescriptor.AutoCloseOutputStream(pipe[1]);
PipeThread pipeThread = new PipeThread(fileContents, stream);
pipeThread.start();
return pipe[0];

For images I use this:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, uri);

I'm creating then a chooser for this intent as usual that's not the issue.. My problem is that although I see for example the Photos app in the chooser, I just cannot open the file in it..It just only opens the gallery with my images. It's working when I use the action send, apps like gmail, drive, dropbox,..all of them are able to correctly read the image from the provider. Also Skitch seems to be the only one app which I have tested it on that is able to open the image also using the Intent.ACTION_VIEW action..

Please don't tell me I should just send the URI, I really need to provide the file as a stream, or somehow as a serie of bytes (IPC limitations would be probably against this). I can't save the file to a public directory.

Was it helpful?

Solution

So the issue was that have been setting Intent type and data in two separate method calls.. What I didn't know is that Intent.setType() clears its data and Intent.setData() clears its type..

When I set both data and type through the Intent.setDataAndType() method call, it works even for URI pointing to a stream.

Unfortunately the final implementation is still not working flawlessly everywhere. It works in default android gallery app, in G+ Photos app, in QuickPic, in Sony gallery app, but it does not work in default HTC gallery neither in default Samsung gallery. Its just a pity, that its actually not that much dependent on my implementation as on how is it implemented in the 3rd party viewer app.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top