Android: Where should I save temporary files?

后端 未结 1 1226
伪装坚强ぢ
伪装坚强ぢ 2021-02-09 03:45

My application allows users to create and modify files. I would like them to be able to send a file as an email attachment. So, I need to first create and write to a temporary f

1条回答
  •  长情又很酷
    2021-02-09 04:28

    You can use Context's getExternalCacheDir() method to get a File reference where you can store files on an SD card. Of course, you'll have to do the usual checks to make sure the external storage is mounted and writable, as usual, but this is probably the best place to store that type of temporary file. One thing you might want to do is just set a maximum amount of space that you can use in the cache directory, and then, any time you need to write a new temporary file, if that file exceeds the maximum space, then start deleting the temp files, starting with the oldest, until there is sufficient space.

    EDIT: Alternatively, maybe something like this would work:

    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        File externalRoot = Environment.getExternalStorageDirectory();
        File tempDir = new File(externalRoot, ".myAppTemp");
    }
    

    Prepending the "." should make the folder hidden, I'm fairly sure.

    0 讨论(0)
提交回复
热议问题