darewreck darewreck - 1 month ago 10
Android Question

Android sharing between apps using ContentUri

I wanted to know the best approach to passing information from an app to another app on the same devices in android.

For example:


  1. I open google apps and I share a document with my App A.

  2. Google App generated an intent and sends a content URI. From my
    understanding, the content uri contains information about the file
    (filename, file size, mimetype) and the ability to extract the
    content which is located in the cache of the google app on the
    device.

  3. When App A opens, it reads the content URI. Ideally, it
    should be able to extract the information from the content uri and
    then render the image. What this means is that App A will display the image shared. In this example, google app shares a docement, and App A wants to open and display the document within it's own app.



The confusing part



it seems that, ideally you should never assume that you can extract the file path and that google has made some updates that makes this not possible.

Work around:


  1. Eventhough i'm not able to extract the file path from the
    contentUri, I'm able to read the bytes of what the contentUri is
    pointing to. So I could save it to a file that is relevant to the
    local cache of App A and pass that path along to get render or pass
    the bytes back. This refers to App A displaying the content. That is passing the path or bytes and let's make the assumption that it knows how to display it given that information.



Question:


  1. The work around does not seem ideal because technically you are
    save the file again on the device. There are two locations with the
    same content ( google app storage and App A's storage). You also
    have to manage when to delete the App A's file that you created.
    This doesn't really seem ideal and was wondering what the best
    approach would be? Or is this the expected flow?

  2. Also I don't know
    if it's ideal to pass the bytes back up vs. just a file path.



Update



To be more specific, the app i'm creating is a hybrid where i'm using cordova plugin to interact with a web app. The web app has methods to process or display the shared document based on file path. So ideally I want to keep it consistent with just reading the file path so that the other platforms that the web app supports does not break.

Any advice appreciated,
D

Answer

Eventhough i'm not able to extract the file path from the contentUri, I'm able to read the bytes of what the contentUri is pointing to.

Correct. This is not significantly different than how you use an HTTPS URL, where you also do not have direct filesystem access to the content (in that case, resident on a different server).

So I could save it to a file that is relevant to the local cache of App A and pass that path along to get render or pass the bytes back.

Or, just consume the bytes. Again, drawing an analogy to an HTTPS URL, there is no requirement to save those bytes to disk to use them.

The work around does not seem ideal because technically you are save the file again on the device. There are two locations with the same content ( google app storage and App A's storage). You also have to manage when to delete the App A's file that you created.

Then do not save the file again on the device, and simply use the stream of bytes. Again, this is not significantly different than using an HTTPS URL.

This doesn't really seem ideal and was wondering what the best approach would be?

Do not write the bytes to disk. Just use them.

So ideally I want to keep it consistent with just reading the file path so that the other platforms that the web app supports does not break.

Your choices are:

  • Improve the Web app code, such that a local file path is one possible source of the data, or

  • Suffer the problems with making copies of that data

After all, bear in mind that the Uri you are given via ACTION_SEND does not have to be a content Uri. It could very easily be an http or https Uri.