Android get free size of internal/external memory

前端 未结 13 1156
眼角桃花
眼角桃花 2020-11-22 15:20

I want to get the size of free memory on internal/external storage of my device programmatically. I\'m using this piece of code :

StatFs stat = new StatFs(En         


        
13条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 15:37

    This is the way i did it..

    internal Total memory

    double totalSize = new File(getApplicationContext().getFilesDir().getAbsoluteFile().toString()).getTotalSpace();
    double totMb = totalSize / (1024 * 1024);
    

    Internal free size

     double availableSize = new File(getApplicationContext().getFilesDir().getAbsoluteFile().toString()).getFreeSpace();
        double freeMb = availableSize/ (1024 * 1024);
    

    External free and total memory

     long freeBytesExternal =  new File(getExternalFilesDir(null).toString()).getFreeSpace();
           int free = (int) (freeBytesExternal/ (1024 * 1024));
            long totalSize =  new File(getExternalFilesDir(null).toString()).getTotalSpace();
            int total= (int) (totalSize/ (1024 * 1024));
           String availableMb = free+"Mb out of "+total+"MB";
    

提交回复
热议问题