4

I develop an app which collects some data from internet. Then save it to a temporary folder. To build this app I need to create and access a folder ( just for the purpose of app, not for the user). How can I do it?

Dewsworld
  • 13,367
  • 23
  • 68
  • 104

5 Answers5

4

this code is to create folder:

File direct = new File(Environment.getExternalStorageDirectory() + "/New Folder");

   if(!direct.exists())
    {
        (direct.mkdir()) //directory is created;

    }

try it may help you

Abhi
  • 8,935
  • 7
  • 37
  • 60
1
File mFile;

onCreate()

mFile= new File(Environment.getExternalStorageDirectory()+"/temp/";

mFile.mkdir();

onDestroy();

mFile.delete();
jazz
  • 1,216
  • 7
  • 7
  • 1
    there's no guaranty onDestroy is going to get called. http://developer.android.com/reference/android/app/Activity.html#onDestroy() – Mariano Latorre Jul 17 '13 at 16:15
1

try out this...

private void makeFolder(){

File root = new File(Environment.getExternalStorageDirectory()
        + File.separator + getString(R.string.folder_name));
    boolean mainfolderexist = root.exists();
        if (!mainfolderexist) {
            try {
                if (Environment.getExternalStorageDirectory().canWrite()) {
                    root.mkdirs();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
}

All The best

Richa
  • 3,165
  • 1
  • 22
  • 26
0

You should really check this other SO answer: https://stackoverflow.com/a/6485850/65716

Aside from the fact that you have to completely manage your use of the space, etc, caching on external storage requires more permission for your app.

See http://developer.android.com/reference/android/content/Context.html#getCacheDir()

"Apps require no extra permissions to read or write to the returned path, since this path lives in their private storage."

Community
  • 1
  • 1
Dave T.
  • 1,368
  • 1
  • 12
  • 17
  • That was creating temp file. Not quite the same as what is being ask here though... – Yuchen Mar 09 '16 at 21:40
  • @YuchenZhong on the contrary, because the question stated "Then save it to a temporary folder" and "just for the purpose of app, not for the user" would drive you to a cache file. The accepted answer is fraught with many pitfalls and potential space-hogging bugs. – Dave T. Mar 10 '16 at 01:43
0

For app use only, I would recommend to use Context.getDir() for retrieving the directory if the files is used by our app only and don`t want to be visible to users by file browsers.

// No need to check if exist, created automatically.
File tempRoot = context.getDir("temp", Context.MODE_PRIVATE); 

// do something
Larry Hsiao
  • 55
  • 1
  • 6
  • and how about creating file under that temp. is it allowable? or denieable ? @LarryHsiao – gumuruh May 29 '20 at 03:11
  • Yes is allowable, no permission required. It will be created at the apps internal file path. like `/data/user/0/package_name/app_temp/`. To create a file, `new File(tempRoot, "fileName").createNewFile();` – Larry Hsiao May 29 '20 at 08:21
  • i should try one ... ok @Larry – gumuruh May 30 '20 at 02:52