1

I would like to use the internal storage for my files. I use function getExternalFilesDir() which uses the internal storage only when there is no SD card mounted, otherwise it stores the files to SD card.

Is there a way how to use only internal storage (that which is accessible for user) even when is SD card mounted?

Thanks

Jan Hakl
  • 75
  • 1
  • 9
  • Possible duplicate of [How to get file path of file from internal storage in android](http://stackoverflow.com/questions/20280250/how-to-get-file-path-of-file-from-internal-storage-in-android) – Michael Spitsin Jul 27 '16 at 09:11

2 Answers2

2

getExternalFilesDir() will provide external storage path not internal to get internal storage path you have to use this method getFilesDir() check below code.

File file = new File(ctx.getFilesDir()+"/MyFolder");
file.mkdirs()
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
1

As per Android Training documentation you can use Internal storage only for

  1. It's always available.
  2. Files saved here are accessible by only your app .
  3. When the user uninstalls your app, the system removes all your app's files from internal storage.

You can use internal storage as follows

File file = new File(context.getFilesDir(), filename); //Creates a new file 

Or you can follow this

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
LvN
  • 701
  • 1
  • 6
  • 22
  • Yes, when I use the getFilesDir() then I store the file to internal storage but user cant access it. But when I use getExternalFilesDir() and there is no SD card then it also use the internal storage and it is accessible for user. I need to use this internal storage always and have it accessible for user in the file manager. – Jan Hakl Jul 27 '16 at 09:32
  • But from this statement "Files saved here are accessible by only your app ." I don't think you can access app files from file manager – LvN Jul 27 '16 at 09:40
  • Yes, but when I use the getExternalFilesDir() and the SD card is not present, then it works... – Jan Hakl Jul 27 '16 at 09:47
  • Some device comes with only internal storage Like LG Nexus 5, in those device to you can save files to the external storage by using getExternalFilesDir() and for internal same getFilesDir(). The difference is that, there is no physical sd card in the device but still have access to external storage. – LvN Jul 27 '16 at 09:50