14

I have an application that saves files downloaded from a server. These files are not private to my application and should be accessible to other applications as well. I want to know what would be the correct path to save these files when the SD card is ABSENT. For the SD card, there is the well known API -

getExternalStorageDirectory()

For the application's private data in the internal memory there is -

Context.getFilesDir()

On some devices, the internal memory is represented by /emmc/.

It will be really helpful if someone could elaborate on /emmc/. I know it stands for embedded Memory card and is not present in all the devices. But is it really representative of the internal memory? Or is it the third memory?

Should I save the files using openFileOutput() with MODE_WORLD_READABLE?

rene
  • 41,474
  • 78
  • 114
  • 152
Akshay
  • 5,747
  • 3
  • 23
  • 35

2 Answers2

15

I realize this is old and has an accepted answer, but I think the question is still relevant and that there is a way to achieve what the asker wants.

To paraphrase, I believe the asker wants to save a file, and make it accessible (e.g. for viewing) by other apps, without having to think about external storage (e.g. an additional permission and the possibility that the external storage is not present).

It turns out that you can make your own files readable by other apps.

File file = new File( ctx.getCacheDir(), "picture" );               
...
file.setReadable( true, false );
  // makes file readable to other apps

// Get another app to view my picture
Uri uri = Uri.fromFile(file);          
Intent intent = new Intent( Intent.ACTION_VIEW );
intent.setDataAndType( uri, "image/*" );
ctx.startActivity( intent );

The setReadable made the above code work for me - without that line the viewer app couldn't read the file. Note that setReadable operates on the whole directory, so the best solution is probably to create a sub-directory name 'public' and put your files in there.

Tom
  • 17,103
  • 8
  • 67
  • 75
  • 3
    Your a lifesaver! The accepted answer focuses too much on why you shouldn't do this and not the actual how. – Ifrit Jan 15 '16 at 19:21
8

On some devices, the internal memory is represented by /emmc/.

/emmc/ may exist on some devices, and it may be internal, but it may not be accessible to applications, and it certainly is not part of the Android SDK unless that happens to be what getExternalStorageDirectory() returns.

But is it really representative of the internal memory?

No.

Or is it the third memory?

Ask your device manufacturer.

Should I save the files using openFileOutput() with MODE_WORLD_READABLE ?

That is impossible to answer in the abstract. You say that your files "should be accessible to other applications as well", but you have not indicated why you expect any other application to care one bit about your files. Other applications will not be scanning your directories for files -- at best, they will allow users to browse external storage. The only reason to have a file that is MODE_WORLD_READABLE is if your application will be triggering another application to do something with the file (e.g., ACTION_VIEW Intent).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks CommonsWare! Yes, my application triggering other applications with ACTION_VIEW is exactly the reason I call these files public. Also, as some file explorers and applications scan the SD card as well as the internal memory, I want them to be able to find these files. So, my question remains, where to save the file in case the SD card is not found? – Akshay Apr 23 '11 at 14:33
  • @Akshay: "Also, as some file explorers and applications scan the SD card as well as the internal memory, I want them to be able to find these files." -- that is not possible, or if it is, it is a major security hole. "So, my question remains, where to save the file in case the SD card is not found?" -- `getFilesDir()`. – CommonsWare Apr 23 '11 at 14:39
  • Thank you CommonsWare! I am trying to understand this clearly. My application can be compared to an email client that downloads attachments and allows document viewers and media players to open these files. Where would an email client save the files if the SD card is absent? Is there no public directory in the internal memory? Something like /internal_memory/downloads ? Why would you consider scanning the internal memory as a security hole? Just to confirm, internal memory implies device/phone memory, right? Sorry for the flurry of questions. – Akshay Apr 23 '11 at 14:51
  • @Akshay: "Where would an email client save the files if the SD card is absent?" -- `getFilesDir()`, or not download them unless the SD card is present. "Is there no public directory in the internal memory?" -- no. "Why would you consider scanning the internal memory as a security hole?" -- because that's not supposed to be possible. – CommonsWare Apr 23 '11 at 15:12
  • 2
    Actually the accepted answer doesn't answer the main question –  Dec 09 '15 at 09:35