如何在同一台设备上运行Android应用程序之间传输文件? [英] How to transfer files between Android applications running on the same device?

查看:111
本文介绍了如何在同一台设备上运行Android应用程序之间传输文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个Android应用程序与基于REST的服务接口。此Web服务本质方面的文件系统,并提供了元数据,以及CRUD对文件的访问。我的应用程序检索的元数据,并通过的ContentProvider 暴露给第三方应用程序。

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.

我需要补充的能力第三方应用程序,同样的设备为我的应用程序上运行,通过请求/从我的应用程序CRUD的实际文件(而不是直接与服务器)。这意味着他们需要要么通过我的应用程序发送或接收的文件的内容(这是典型的XML或图像)。

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:

这似乎是给了第三方应用程序,从我的的ContentProvider 读取文件的能力的不二选择。我认为,它开始变得棘手时,这些应用程序需要创建或通过我的`ContentProvider的更新文件。当他们为了知道什么时候新的/更改的文件发送回服务器完成我需要一个回调。我相信我可以用一个 FileObserver 用于这一目的,但。

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.

通过这种方式,我可以通过使者送我的应用程序和客户端应用程序之间的文件。该文件必须通过一个捆绑传递,所以我不知道什么是最好的格式传输他们(文件的FileDescriptor ,字节数组,其他什么东西?)。我没有关于这是否会造成问题,如果这些文件得到很大一个很好的处理。

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.

  1. 使用文件夹(S)上的外部存储作为一个下拉框
  2. 在沟通的CRUD请求,并且下拉框内容,通过使者 / 服务
  3. 使用的ContentProvider 来存储请求的状态
  4. 在第三方应用程序通过 ContentObserver
  5. 接收状态更新
  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

我觉得用的ContentProvider 将是理想的解决方案,但似乎API不完全支持我的使用情况。我很担心,试图走这条路可能会导致缺憾的实现。如果我去了使者服务的办法,我是不确定的最可靠的方法,将文件通过传输捆绑

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.

的混合方法似乎pretty的健壮的,但最复杂的实施。文件实际上没有被传来传去,这样的表现应该不错。不过,我担心这是过度架构的解决方案。

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.

什么是相同的Andr​​oid设备上运行的应用程序之间传输文件的最佳方法?当然,我愿意,我在我的问题还没有列出其他的选择。

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.

推荐答案

内容提供商肯定是要走的路。如果你认为谷歌使用了这种方法几乎都那么它就变成appaentr,这是预期的设计方法。

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.

更新

还有就是如何做到这一点的CommonsWare本书,看到该链接提供了一个示例。

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

的内容提供商源/文件

使用为内容提供商的同步框架。简单地保持请求的列表,然后安排同步下载这些文件。你也可以这样做网络发痒等,则可以使用广播意图或contentobserver通知客户端下载该文件。

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.

在本质上,这可能是类似于你的第三选择,但重要的是它采用的是Android提供的工具,而不是滚动您自己。

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

广告Endum

开始最好的地方是Android SDK样本:Android的SDK \样本\ Android为8 \ SampleSyncAdapter,但被警告有接触的负载相关的东西,掩盖了多汁位。我花了一段时间才能弄清楚,我可以几乎全部删除除syncadapter

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

这篇关于如何在同一台设备上运行Android应用程序之间传输文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆