问题描述
我要保存一个文件,然后触发一个意图的其他应用程序打开该文件。我如何做到这一点?
I want to save a file and then fire an intent to an other application to open that file. How do I accomplish this?
如果尝试: openFileOutput(file.pdf,Context.MODE_WORLD_READABLE)
,但似乎并没有工作。我应该保存应用程序文件夹以外的文件?如果是的话我怎么把它传递正确的权限?
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?
推荐答案
您可以使用这样的:
String extPath = Environment.getExternalStorageDirectory().getAbsolutePath();
String pathPrefix = extPath + "/Android/data/" + APP_PACKAGE_ID + "/cache/";
将文件保存到SD卡上,这是由所有其他应用程序进行访问。该模式 Context.MODE_WORLD_READABLE
就足够了这一点。在code是与Android 2.1的用途和使用存储在SD卡上的应用程序相关的数据建议的路径。启动了Android 2.2,如果应用程序卸载该目录将自动被删除。
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.
您的应用程序需要安装到SD卡上正确的:
Your app needs the right to install to sd card:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
使用 openFileOutput(...)
与世界可读权限是有点用处,因为这文件存储到一个文件夹只能由应用程序本身进行访问。
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.
请注意,外部存储器可以是不可用的,如果用户已经通过USB连接的设备进行文件存储访问。你应该总是通过检查此条件第一个String状态= Environment.getExternalStorageState();
。
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();
.
这篇关于Android的 - 如何保存一个文件,该文件是可读的其他应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!