Android - How do I save a file that is readable by an other app?

大憨熊 提交于 2019-12-01 04:19:39

问题


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?


回答1:


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();.



来源:https://stackoverflow.com/questions/7710486/android-how-do-i-save-a-file-that-is-readable-by-an-other-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!