Android - How to work around a “no space left on device” error

夙愿已清 提交于 2019-12-06 07:22:28

you are actually using internal memory by using getCacheDir() in your else statement. However, if you want to store large data then its recommended to use getExternalCacheDir() because not all android phones have huge internal storage, but they do/may have bigger external storage.

if (sdState.equals(android.os.Environment.MEDIA_MOUNTED)) { 
   File sdDir = context.getExternalCacheDir();
   cacheDir = new File(sdDir,"data"); 
} 
else 
   Log.e("storage", "SD card not found");

I ended up changing my code where I create my directories to the following:

  String sdState = android.os.Environment.getExternalStorageState();
    if (sdState.equals(android.os.Environment.MEDIA_MOUNTED)) {
        File sdDir = android.os.Environment.getExternalStorageDirectory();      
        cacheDir = new File(sdDir,"data/gr");
    }
    else
        cacheDir = context.getExternalFilesDir(sdState); <--changed

    if(!cacheDir.exists())
        cacheDir.mkdirs();


    if(sdState.equals(android.os.Environment.MEDIA_MOUNTED)){
        File adSdDir = android.os.Environment.getExternalStorageDirectory();
        adCacheDir = new File(adSdDir,"data/ad");
    }else
        adCacheDir = context.getExternalFilesDir(sdState); <--changed

    if(!adCacheDir.exists())
        adCacheDir.mkdirs();
}

So far using this I am able to once again save images I take with the camera with all the images in my area download.

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