3

I would like to get a path to public storage of my android device. I have two applications. One is writing some log files and the other one is used to read them (for this reason I cant use application private storage). I would like to know if there is a method, that will give me path of the "public space" of my device where I can easly create and read files.

This solution is not what I am looking for:

http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

Neither is that:

How to get the internal and external sdcard path in android

Is there a easy solution for my problem? Is the external public storage what I am looking for?

http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

The problem is that when I run application on my device everything is ok, but then when I run it on device where there is no memory card, it is not working. So I would like to use external public storage, that is not a memory card...

My code below doesn't work (it doesn't save the file). When I choose directory that is directly in Environment.getExternalStorageDirectory().getAbsolutePath(), it work... What I do wrong? :

transient private final String DIRECTORY = "/Android/data/com.aaa.bbb.ccc/files/";

public void writeLog()
{
    Calendar calendar = setDate();

    File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + DIRECTORY);
    if(!dir.exists())
        dir.mkdir();

    File file = new File (dir, "log_" + calendar.get(Calendar.YEAR) + "_"
                                 + ((calendar.get(Calendar.MONTH))+1) + "_"
                                 + calendar.get(Calendar.DAY_OF_MONTH)
                                 + ".dta");

...

}

UPDATE:

Why this code works:

    File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Dir1/");
    if(!dir.exists())
        dir.mkdir();

And this doesn't

    File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Dir1/Dir2/Dir3/");
    if(!dir.exists())
        dir.mkdir();
Community
  • 1
  • 1
Marek
  • 3,935
  • 10
  • 46
  • 70

2 Answers2

1

try looking at android.os.Environment here.

Following methods seem interesting..

public static File getDataDirectory ();
//Gets the Android data directory. 

public static File getDownloadCacheDirectory ();
//Gets the Android download/cache content directory.  

EDIT: *Solution:*

a small mistake you were making is then

replace this :

if(!dir.exists())
    dir.mkdir();

with:

if(!dir.exists())
    dir.mkdirs(); // will create all the parent directories as well..

hope i helped you now with

mn0102
  • 839
  • 1
  • 12
  • 25
  • Why that method is not good: getExternalStorageDirectory().getAbsolutePath and why my code is not working... it should work – Marek May 30 '13 at 05:41
  • in your code, problem is when there is no SDCard. so i suggested you to find ways to write the file on phone memory. If sdcard is not present how can you write into it? – mn0102 May 30 '13 at 05:55
  • The code that I use is to write in phone memory visible as SD card (I have Galaxy S3). The problem is different. When I use DIRECTORY as it is writen now, it doesnt work. But when I put in DIRECTORY only one directory, like DIRECTORY = "/SomeDir/" it works perfectly.... why? – Marek May 30 '13 at 06:01
  • I just found that mistake in the same moment you posted. But thanks! Let me check if reading is also working – Marek May 30 '13 at 06:13
  • yeah, nothing can stop you for now! – mn0102 May 30 '13 at 06:40
  • on a android 4.4.2 emulator with no sdcard i get permission denied error if i try to write to getDownloadCacheDirectory() EACCESS (Permission denied) when i open a file for writing "/cache/test.tmp". getDataDirectory() is also write protected. on android 2.1 emulator it works. – k3b Nov 17 '14 at 08:23
1

In some devices the external sdcard default name is showing as extSdCard and for others it is sdcard1. This code snippet helps to find out that exact path and helps to retrieve the path of external device.

String sdpath, sd1path, usbdiskpath, sd0path; 
if (new File("/storage/extSdCard/").exists()) {
    sdpath="/storage/extSdCard/";
    Log.i("Sd Cardext Path", sdpath);
}
if (new File("/storage/sdcard1/").exists()) {
    sd1path="/storage/sdcard1/";
    Log.i("Sd Card1 Path", sd1path);
}
if (new File("/storage/usbcard1/").exists()) {
    usbdiskpath="/storage/usbcard1/";
    Log.i("USB Path", usbdiskpath);
}
if (new File("/storage/sdcard0/").exists()) {
    sd0path="/storage/sdcard0/";
    Log.i("Sd Card0 Path", sd0path);
}
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
Sam
  • 335
  • 3
  • 4