2

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

Waypoint
  • 17,283
  • 39
  • 116
  • 170

1 Answers1

8

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.

Chirag
  • 56,621
  • 29
  • 151
  • 198
  • Thanks, but I have Samsung Android Phone and External Storage tells me storage, which is built into phone. I am not able to find location and capacity of inserted SD card. – Waypoint Feb 01 '13 at 11:27
  • In short, 'external storage' is more like 'shared storage' and it may or may not be implemented by an actual SD card. Some devices have an additional SD card, not used as external storage. If that is what you are asking for, there is currently no public API for accessing its mount location, and it varies between devices. – Chirag Feb 01 '13 at 11:31