如何在内容提供商中存储文件

发布于 2024-11-18 00:42:45 字数 505 浏览 1 评论 0原文

我需要将 Zip 文件附加到 Android 电子邮件编辑器中的消息中。

Zip 文件由我的应用程序创建,并存储在应用程序的私有存储区域(可通过 getFilesDirectory() 访问)。 我正在获取此 Zip 文件的 URI 并将其附加到电子邮件意图对象中;当我启动电子邮件活动时,我可以看到附件,但我的电子邮件收件人没有收到文件,

对此进行一些研究后,我发现我的应用程序数据无法与其他应用程序共享(在本例中为 Android 电子邮件应用程序) 。

为了解决这个问题,我想实施使用内容提供商来共享我的应用程序数据的推荐解决方案。

首先,我想确认这是否可能,如果可能的话,有人可以给我一些关于如何去做这件事的提示。我需要知道如何将 Zip 文件从应用程序的 getFilesDirectory() 复制到内容提供程序,以及如何将内容提供程序 URI 附加到电子邮件意图对象。

我无法将我的 zip 文件放入 SD 卡中...

我只想将我的文件存储到我的设备内部存储中并附加到电子邮件编辑器。

I have a requirement to attach a Zip file to a message in the Android email composer.

The Zip file is being created by my application and stored in the app's private storage area (accessible via getFilesDirectory()).
I am obtaining the URI to this Zip file and appending it to an email intent object; when I launch the email activity I am able see the attachment but my email recipient is not receiving the files,

After doing some research on this, I found that my application data cannot be shared with other app's (in this case the Android Email app).

To solve this problem I would like to implement the recommend solution of using a content provider to share my application data.

First of all, I would like to confirm if this is possible, and if it is could anyone please give me some hints on how to go about doing this. I need to know how to copy my Zip file from the getFilesDirectory() of my application, to the content provider, and also how to attach the content provider URI to the email intent object.

I can't place my zip files into an SD card...

I just want only to store my files in to my device internal storage and attach to the email composer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹苦笑 2024-11-25 00:42:45

默认情况下,设备上的任何应用程序都可以访问 ContentProvider。如果您愿意设备上的任何应用程序访问这些文件,请创建一个 ContentProvider,其中包含 getType()openFile()< 的实际实现/代码>。然后,content:// URL 应该适用于电子邮件应用程序,AFAIK。

这是一个示例项目,演示了 ContentProvider 提供文件从本地存储,在本例中为 WebView

By default, a ContentProvider can be accessed by any application on the device. If you are willing for these files to be accessed by any application on the device, create a ContentProvider, with real implementations for getType() and openFile(). Then, the content:// URL should work with the Email app, AFAIK.

Here is a sample project demonstrating a ContentProvider serving up files from local storage, in this case to a WebView.

猫七 2024-11-25 00:42:45

如果您只想避免在外部 SD 卡上进行存储,那么您不需要 ContentProvider。您可以接受

openFileOutput("yourzipfile.zip", MODE_WORLD_READABLE)

并传递

putExtra(Intent.EXTRA_STREAM, Uri.fromFile(getFileStreamPath("yourzipfile.zip))

您的 ACTION_SEND 意图。

但是,电子邮件程序可能不会设置内容类型。

此外,没有可靠的方法来判断电子邮件应用程序何时不再需要您的文件。这意味着您要么面临收到许多文件的风险,要么将较新的内容作为旧电子邮件的附件发送。

此解决方案的另一个问题是每个人都可以读取您的 zip 文件。如果使用 ContentProvider 解决方案,则不会出现此问题,您可以在每个 Intent 的基础上授予访问权限,即仅允许一个电子邮件 Intent 访问一个文件。

然后通过 URI 完成 Uri 的匹配,该 URI 可能以您的包名称开头,例如

content://com.yourdomain.yourapp.yourproviderclass/some/path

您可能需要查看 http://developer.android.com/guide/topics/manifest/provider-element.html

If all you want is to avoid storage on the external SD card, then you don't need a ContentProvider. You can live with

openFileOutput("yourzipfile.zip", MODE_WORLD_READABLE)

and pass

putExtra(Intent.EXTRA_STREAM, Uri.fromFile(getFileStreamPath("yourzipfile.zip))

along with your ACTION_SEND Intent.

However, the email program will probably set no Content-type.

Also, there's no reliable way to tell when the email app doesn't need your file anymore. This means that you either risk ending up with many files or sending a newer content as the attachment of an older email.

Another issue with this solution is that everyone can read your zip file. This problem would not occur with a ContentProvider solution, where you can grant access permission on a per-Intent basis, i.e. allowing access to one file only for one email Intent.

Matching of the Uri is then done via the URI, which may start with your package name, such as

content://com.yourdomain.yourapp.yourproviderclass/some/path

You may want to look at http://developer.android.com/guide/topics/manifest/provider-element.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文