开发者

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

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_READ开发者_开发问答ABLE) 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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜