Android - 如何保存其他应用程序可读的文件?

发布于 2024-12-09 01:00:07 字数 179 浏览 1 评论 0原文

我想保存一个文件,然后向其他应用程序发出意图以打开该文件。我该如何实现这个目标?

如果尝试过: openFileOutput("file.pdf", Context.MODE_WORLD_READABLE) 但似乎不起作用。我应该将文件保存在应用程序文件夹之外吗?如果是这样,我如何向其传递正确的权限?

I want to save a file and then fire an intent to an other application to open that file. How do I accomplish this?

If tried : openFileOutput("file.pdf", Context.MODE_WORLD_READABLE) but doesn't seem to work. Should I save the file outside the applications folder? If so how do I pass it the correct permissions?

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

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

发布评论

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

评论(1

上课铃就是安魂曲 2024-12-16 01:00:07

您可以使用这样的方法:

String extPath = Environment.getExternalStorageDirectory().getAbsolutePath();
String pathPrefix = extPath + "/Android/data/" + APP_PACKAGE_ID + "/cache/";

将文件保存到 SD 卡,所有其他应用程序都可以访问该卡。模式 Context.MODE_WORLD_READABLE 足以满足此目的。该代码适用于 android 2.1,并使用建议的路径在 SD 卡上存储应用程序相关数据。从 Android 2.2 开始,如果卸载应用程序,此目录会自动删除。

您的应用程序需要安装到 SD 卡的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

使用具有全局可读权限的 openFileOutput(...) 有点无用,因为这些文件存储在只能由应用程序本身访问的文件夹中。

数据存储文档中介绍了更多信息。

请注意,如果用户通过 USB 连接设备进行文件存储访问,则外部存储器可能不可用。您应该始终首先通过 String state = Environment.getExternalStorageState(); 检查此条件。

You can use something like this:

String extPath = Environment.getExternalStorageDirectory().getAbsolutePath();
String pathPrefix = extPath + "/Android/data/" + APP_PACKAGE_ID + "/cache/";

to save the file to sd card, which is accessible by all other applications. the Mode Context.MODE_WORLD_READABLE is sufficient for this. The code is for use with android 2.1 and uses the suggested path for storing app related data on the sd card. Starting with Android 2.2 this directory automatically gets deleted if the app is uninstalled.

Your app needs the right to install to sd card:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Using openFileOutput(...) with world readable rights is a bit useless as this files are stored into a folder only accessible by the application itself.

More information is described in the data storage documentation.

Please note that the external memory may be unavailable if the user has connected the device via USB for file storage access. You should always check for this conditions first via String state = Environment.getExternalStorageState();.

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