What is the best way to create temporary files on Android?

前端 未结 3 646
南旧
南旧 2020-12-09 19:25

Well, this is not exactly a question, as I\'m not really \"stuck\" on my code, but I\'ve found some strange behavior for Android API regarding accessing the external storage

3条回答
  •  攒了一身酷
    2020-12-09 19:28

    you're getting this error because you declare on the manifest.xml permission to write to the external storage. you need to add this permission.

    also, do never ever ever ever write files (specially temporary ones) directly on the getExternalStorage().

    This will put directly on the SD-card and will create a mess. For temporary files that is only to be seen by your own application you should use:

    getExternalStorage() + "/Android/data//cache/"
    

    replacing by your app packaged, e.g. com. Orabig.myFirstApp that special folder is automatically deleted from the system if the user uninstall the application, keeping the system free from temporary files.

    edit:

    Please note that my manifest does not include the

    you have to!

    edit: also, if you creating temporary media files (PNG for example) is good practice to create an empty file named .nomedia on that folder. That way you avoid the Media Scanner scanning it and showing it on the gallery.

    last edit:

    and before creating files you must create the folder by calling mkdirs() on the File object.

提交回复
热议问题