Android - get list of all available storage

血红的双手。 提交于 2019-12-08 02:12:03

问题


I am downloading data into my app from the internet. If I specify internal memory (=Environment.getExternalStorageDirectory()), I may have face problem of "not enough space". Sdcard mount address always differs phone to phone, so I would like to allow user to select his preferable location, where to store downloaded data. Is there any method, how to get all available storage options, whhich can be used? (generally, I would like to choose between internal memory or inserted sdcard).

Thanks


回答1:


Please check the below code.

Calculate Available Internal Storage memory

StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
long bytesAvailable = (long)stat.getFreeBlocks() * (long)stat.getBlockSize();
long megAvailable = bytesAvailable / 1048576;

Calculate Total Internal Storage memory

StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount();
long megAvailable = bytesAvailable / 1048576;

Calculate Available External Storage memory

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable = (long)stat.getFreeBlocks() * (long)stat.getBlockSize();
long megAvailable = bytesAvailable / 1048576;

Calculate Total External Storage memory

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount();
long megAvailable = bytesAvailable / 1048576;

Note : First of all you need to calculate avilable internal memory and show to user where he/she wants to download file.



来源:https://stackoverflow.com/questions/14645189/android-get-list-of-all-available-storage

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