问题
Access USB Device Memory through programmatically.
- Get USB Device Total Memory Size.
- Get USB Device Total Free(Available) Memory Size.
I know the memory size of Internal/External Memory
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
String availableMemory = Formatter.formatFileSize(this, availableBlocks * blockSize);
Log.i("","Available MB : " + availableMemory);
In this code does not worked for identifying the USB Device Memory.
How do find the USB Device Memory.
Thanks in advance.
回答1:
Hi Please use below code...
StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
long blockSize = statFs.getBlockSize();
long totalSize = statFs.getBlockCount()*blockSize;
long availableSize = statFs.getAvailableBlocks()*blockSize;
long freeSize = statFs.getFreeBlocks()*blockSize;
And If you fine in MB then
long megAvailable = availableSize/ (1024 * 1024);
Log.e("","Available MB : "+megAvailable);
回答2:
Use this helper class that uses the StatFs Class:
public class MemoryUsage{
/*
Returns size in MegaBytes.
*/
public int TotalMemory()
{
StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576;
return Total;
}
public int FreeMemory()
{
StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
return Free;
}
public int BusyMemory()
{
StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576;
int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
int Busy = Total - Free;
return Busy;
}
}
If you need calculate internel memory, change this:
StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
to this:
StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
回答3:
You need to be more specific than "does not worked".
- What was the behaviour you discovered?
- Did you get an error, or did you get zero?
- Were you using an emulator or a device?
- Were you using a system with multiple external storage?
A good example: Android get free size of internal/external memory
Also, the USB device is difficult to detect, if the default storage is not set to the USB device. All the Environment.* folder/path APIs will return the
Ref: http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()
Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.
And then there is another API that may be what you are looking to check instead of data-directory.
Ref: http://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory(java.lang.String)
Here's Motorola with their specific API for handling such cases, but again that may not be what you are looking for depending on your particular problem: http://developer.motorola.com/docs/motorola-external-storage-api/
In Any case, I hope this would help with your problem. If not, please provide more information and the great guys at StackOverflow will help in their usual great way.
Hope this helps.
来源:https://stackoverflow.com/questions/11451659/android-get-total-size-and-free-size-of-usb-device-memory