开发者

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

开发者 https://www.devze.com 2023-04-12 02:34 出处:网络
I want to save a file and then fire an intent to an other application to open that file. How do I accomplish this?

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

精彩评论

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

关注公众号