Recommended path for caching images on external memory / sd card

泄露秘密 提交于 2019-12-07 12:35:27

问题


When I look at my sd card root directory in Android file transfer or Astro file manager it's a mess because apps are storing files all over the place.

As developers we follow best practice by caching images/files etc to make our apps quicker and we use Environment.getExternalStorageDirectory() to get the root of the external storage (not hardcoding /sdcard). But what should go next? ideal world we'd follow a convention for dumping our cached files on the open field that is the external storage?

Here's a couple of the options I see from my own device/experience:

  • /.cache/app_name/
  • /.app_name/
  • /cache/<app_name>/
  • /data/<app_name>/
  • /com.package.name/
  • /app_name/

I currently prefer the first option as hidden by default from on device file managers.

Does anyone know of recommendations from Google or conventions followed by several popular/big names apps? or reasons this is a bad idea?


回答1:


According to the Developer Docs

Saving cache files

If you're using API Level 8 or greater, use getExternalCacheDir() to open a File that represents the external storage directory where you should save cache files. If the user uninstalls your application, these files will be automatically deleted. However, during the life of your application, you should manage these cache files and remove those that aren't needed in order to preserve file space.

If you're using API Level 7 or lower, use getExternalStorageDirectory() to open a File that represents the root of the external storage, then write your cache data in the following directory:

/Android/data/<package_name>/cache/

The <package_name> is your Java-style package name, such as "com.example.android.app".

More info here

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

Hope it helps




回答2:


The Developer Guide states that files should be stored in the directory /Android/data/<package_name>/files/, which is seemingly where a fair few apps store their files. As an added benefit, files here will get deleted once your app is uninstalled and it doesn't clutter up the root. I'd think putting cache files in /Android/data/<package_name>/cache/ would probably be a good option!



来源:https://stackoverflow.com/questions/16156108/recommended-path-for-caching-images-on-external-memory-sd-card

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!