0

Access USB Device Memory through programmatically.

  1. Get USB Device Total Memory Size.
  2. 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.

Sathish Sathish
  • 2,251
  • 3
  • 20
  • 21

3 Answers3

2

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);
Mehul Mistri
  • 15,037
  • 14
  • 70
  • 94
Nikhil
  • 16,194
  • 20
  • 64
  • 81
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());
K_Anas
  • 31,226
  • 9
  • 68
  • 81
1

You need to be more specific than "does not worked".

  1. What was the behaviour you discovered?
  2. Did you get an error, or did you get zero?
  3. Were you using an emulator or a device?
  4. 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.

Community
  • 1
  • 1
chkdsk
  • 1,187
  • 6
  • 20