Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transfer files between Android applications running on the same device?

Tags:

I am writing an Android application that interfaces with a RESTful service. This web service essentially fronts a file system, and provides metadata as well CRUD access to the files. My application retrieves the metadata, and exposes it to 3rd party apps through a ContentProvider.

I need to add the ability for 3rd party applications, running on the same device as my app, to CRUD the actual files by making requests to/from my app (not directly with the server). This means they need to either send or receive the contents of the files (which are typically XML or images) through my app.

I have thought of two approaches for implementing this:

Option 1 - Using ContentProvider.openFile

This seems like an obvious choice for giving 3rd party applications the ability to read files from my ContentProvider. I think it starts getting tricky when those applications need to create or update files through my `ContentProvider'. I'll need a callback when they are finished in order to know when to send the new/changed file back to the server. I believe I could use a FileObserver for that purpose though.

Option 2 - Using a Messenger through a Service

With this approach, I can send the files between my application and client applications through the Messenger. The files would have to be passed through a Bundle, so I am not sure what the best format is for transmitting them (File, FileDescriptor, byte array, something else??). I don't have a good handle on whether or not this would cause problems if the files get to be large.

Option 3 - a hybrid approach

  1. Use folder(s) on external storage as a drop box
  2. Communicate CRUD requests, and drop box contents, through a Messenger/Service
  3. Use the ContentProvider to store the status of requests
  4. 3rd party app receives status updates through a ContentObserver

Summary

I think using ContentProvider would be the ideal solution, but it seems that the API does not fully support my use case. I am concerned that trying to go down that path might result in a kludgy implementation. If I go with a Messenger and Service approach, I am uncertain of the most robust way to transfer the files through a Bundle.

The hybrid approach seems pretty robust, but the most complex to implement. Files aren't actually being passed around, so performance should be good. However, I fear this is over-architecting the solution.

What is the best approach for transferring files between applications running on the same Android device? Of course, I am open to other options which I have not outlined in my question.

like image 669
Eric Levine Avatar asked Aug 31 '11 14:08

Eric Levine


People also ask

How do I share a folder between Android phones?

To do this, right-click the file or folder you want to share, then click Properties. Go to the Sharing tab, then click Share. On the drop-down menu, select “Everyone,” then click Add. Click on Share at the bottom right.


1 Answers

Content provider is definitely the way to go. If you consider that google uses this approach for almost everything then it becomes appaentr that this is the intended design method.

I'm not extolling the virtues of them, but in the land of the blind, the one eyed content provider is king.

Update

There is an example of how to do this in CommonsWare book, see the link provided.

Source of Content Provider/Files

Use the synch framework for content providers. Simply maintain a list of requests and then schedule the sync to download those file. You can also do this on network tickles etc. you can use broadcast intents or contentobserver to notify clients that the file is downloaded.

In essence this is probably similar to your 3rd option but importantly it uses the Android supplied tools rather than rolling your own.

Ad Endum

Best place to start is the android SDK sample in: android-sdk\samples\android-8\SampleSyncAdapter but be warned that there's a load of contacts related stuff that masks the juicy bits. It took me a while to figure out that I could delete almost all of it except the syncadapter

like image 178
Moog Avatar answered Nov 01 '22 15:11

Moog