5

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?

Jeroen
  • 3,399
  • 1
  • 22
  • 25

1 Answers1

5

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

Janusz
  • 187,060
  • 113
  • 301
  • 369
HefferWolf
  • 3,894
  • 1
  • 23
  • 29
  • 1
    Thanks for the quick answer, but how do I mark the file as `Context.MODE_WORL_READABLE` when I don't use `openFileOutput`? The android docs leave me kind of clueless there as all the Stream classes don't accept that as a parameter. I must be missing something here.... – Jeroen Oct 10 '11 at 09:25
  • 3
    Sorry, my fault: there are no access rights for the sd card, the data you store there is always world writeable. – HefferWolf Oct 10 '11 at 09:45
  • And what about the permissions when there is no SD card? – Jeroen Oct 10 '11 at 10:59
  • 1
    My whole suggestion requires a external storage dir, which usually is a sd card but on devices with big internal storage the "external storage directory" might just be mapped to this, but concerning the permissions, there's still everything word writable. – HefferWolf Oct 10 '11 at 11:29
  • 3
    As of the time of my comment, Context.MODE_WORLD_READABLE is now deprecated. Any other safe way to do this? http://stackoverflow.com/questions/13856757/android-alternative-to-the-deprecated-context-mode-world-readable – Marl Dec 13 '12 at 09:24
  • @Marl The android way of doing this would be a [`ContentProvider`](https://developer.android.com/reference/android/content/ContentProvider.html). Save the files somewhere private and then expose it for example via [`FileProvider`](https://developer.android.com/reference/android/support/v4/content/FileProvider.html). This might be a bit more verbose, but it's also more secure. – schnatterer Jun 22 '15 at 20:48