开发者

Android How to use and create temporary folder

开发者 https://www.devze.com 2023-04-09 09:23 出处:网络
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 fo

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?


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


File mFile;

onCreate()

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

mFile.mkdir();

onDestroy();

mFile.delete();


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


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."


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号