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

前端 未结 3 1952
遇见更好的自我
遇见更好的自我 2021-01-31 17:50

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

相关标签:
3条回答
  • 2021-01-31 18:33

    Using the SD card is definitely the recommended way to go to share files on Android.

    However, I would go with a modified hybrid solution which makes use of startActivityForResult() and onActivityResult() (docs here) on the client side to communicate CRUD requests (and getting the Uri to the file(s) on the SD card if needed) if you don't mind creating a dummy activity as a front end to your service. Clients, once finished with the file(s), can call startActivityForResult() again to alert your app to changes.

    Of course this can be done with startService()/bindService() however it doesn't provide an easy way for clients to obtain a status result especially if you need IPC.

    Although content providers/resolvers feel like the correct way to go about things, I do feel it is more for single direction requests specific to providing/consuming content.

    0 讨论(0)
  • 2021-01-31 18:42

    http://developer.android.com/reference/android/os/ParcelFileDescriptor.html can be sent between processes. I believe that there is a subtly where these are explicitly blacklisted from being allowed to be put in intents. They can be sent through AIDL though. Also, do NOT use the sdcard for this. This is just asking for trouble. One sdcard is world readable, so anyone can see it. Also, you do not always have access to write to the sdcard (it is removed or put in UMS).

    0 讨论(0)
  • 2021-01-31 18:51

    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

    0 讨论(0)
提交回复
热议问题