Summary of android bucket check

Source: Internet
Author: User

 

There were few things in the past few days, so I summarized some of the things I used earlier.

 

The storage space on the android mobile phone mainly includes the storage space of the mobile phone itself and the storage space of the SD card. Previously, I used the most storage space on the SD card. In summary, I checked the source code of the system, which is very rewarding. When an application is installed on a mobile phone, the system automatically creates a directory under the primary packages/data directory with the main package name of the application, by default, a lib directory is created in the last directory. Let's take a look at the code for the system to get the bucket:

 

System source code from android2.3.3/packages/apps/Settings/src/com/android/settings/applications/ManageApplications. java

 

 

531. mDataFileStats = new StatFs ("/data ");

532. mSDCardFileStats = new StatFs (Environment. getExternalStorageDirectory (). toString ());

 

<Pre name = "code" class = "java"> 748. mDataFileStats. restat ("/data ");

Try {

TotalStorage = (long) mDataFileStats. getBlockCount ()*

MDataFileStats. getBlockSize ();

FreeStorage = (long) mDataFileStats. getAvailableBlocks ()*

MDataFileStats. getBlockSize ();

} Catch (IllegalArgumentException e ){

}

Final int N = mApplicationsAdapter. getCount ();

For (int I = 0; I <N; I ++ ){

ApplicationsState. AppEntry AE = mApplicationsAdapter. getAppEntry (I );

AppStorage + = AE. codeSize + AE. dataSize;

}

FreeStorage + = mApplicationsState. sumCacheSizes (); <pre name = "code" class = "java"> <pre>

 

734. mSDCardFileStats. restat (Environment. getExternalStorageDirectory (). toString ());

Try {

TotalStorage = (long) mSDCardFileStats. getBlockCount () * mSDCardFileStats. getBlockSize ();

FreeStorage = (long) mSDCardFileStats. getAvailableBlocks () * mSDCardFileStats. getBlockSize ();

} Catch (IllegalArgumentException e ){

// Use the old value of mFreeMem

}

 

The second line of code is the space of the computing system, and the second line is the space for checking the SD card. We can see that the system checks the space under the/data directory. The mobile phone has a ROM parameter. What space does the ROM mean? For example, if G7 and ROM are 512 MB, how is the space allocated? Fortunately, G7 has the root permission. I don't know if the following operations require the root permission. If you have a mobile phone, try it. Find the directory where the adb.exe file is located in sdk. the files earlier than 2.3 are in the tools directory and later are in the platform-tools directory. If you do not need to configure environment variables, you can directly start the command line window to execute the adb shell, and then enter the system file directory, and then execute df, you will see some information, the following is the G7 information (you can also try it with a simulator:

Filesystem 1K-blocks Used Available Use % Mounted on

Tmpfs 207628 32 207596 0%/dev

Tmpfs 207628 0 207628 0%/mnt/asec

Tmpfs 207628 0 207628 0%/mnt/obb

/Dev/block/mtdblock3 256000 145736 110264 57%/system

/Dev/block/mtdblock5 151168 102396 48772 68%/data

/Dev/block/mtdblock4 40960 1204 39756 3%/cache

/Dev/block/vold/179: 1

942848 49232 893616 5%/mnt/sdcard

/Dev/block/vold/179: 1

942848 49232 893616 5%/mnt/secure/asec

It can be seen that there is a partition mounted to the/data directory, 48772 K has been used, and 102396 K is left, that is, the ROM of 151168 m is allocated to the user only K, less than 150 M, the rest are occupied by the system, and the rest will not be said, do not dare to give an axe in front of the linux Gods. From the code above, we can see that the system still has a certain cache space for the application and is finally computed into the available space. You can use the following code to test whether the space under the/data DIRECTORY And/data directory is different. There is a small difference. It seems like a cache space. The SD card will not be mentioned. From the above information, we can also see that the SD card has 1 GB and is mounted under the/mnt/sdcard directory.

 

The following is the code for checking the bucket of the mobile phone. It is for reference only.

 

 

<Pre name = "code" class = "java"> <pre name = "code" class = "java">

Import java. io. File;

 

Import android. OS. Environment;

Import android. OS. StatFs;

 

/**

* Bucket Management

* @ Author maylian7700@126.com

*

*/

Public class MemorySpaceCheck

{

/**

* Calculate the remaining space

* @ Param path

* @ Return

*/

Private static long getAvailableSize (String path)

{

StatFs fileStats = new StatFs (path );

FileStats. restat (path );

Return (long) fileStats. getAvailableBlocks () * fileStats. getBlockSize (); // note the difference with fileStats. getFreeBlocks ()

}

/**

* Total computing space

* @ Param path

* @ Return

*/

Private static long getTotalSize (String path)

{

StatFs fileStats = new StatFs (path );

FileStats. restat (path );

Return (long) fileStats. getBlockCount () * fileStats. getBlockSize ();

}

/**

* Calculate the remaining space of the SD card

* @ Return remaining space

*/

Public static long getSDAvailableSize ()

{

If (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED ))

{

Return getAvailableSize (Environment. getExternalStorageDirectory (). toString ());

}

Return 0;

}

/**

* Remaining space of the computing system

* @ Return remaining space

*/

Public static long getSystemAvailableSize ()

{

// Context. getFilesDir (). getAbsolutePath ();

Return getAvailableSize ("/data ");

}

Www.2cto.com

/**

* Is there enough space?

* @ Param filePath: the file path, not the directory path.

* @ Return

*/

Public static boolean hasEnoughMemory (String filePath)

{

File file = new File (filePath );

Long length = file. length ();

If (filePath. startsWith ("/sdcard") | filePath. startsWith ("/mnt/sdcard "))

{

Return getSDAvailableSize ()> length;

}

Else

{

Return getSystemAvailableSize ()> length;

}

}

/**

* Get the total space of the SD card

* @ Return

*/

Public static long getSDTotalSize ()

{

If (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED ))

{

Return getTotalSize (Environment. getExternalStorageDirectory (). toString ());

}

Return 0;

}

/**

* Obtain the total space that can be read and written by the system.

* @ Return

*/

Public static long getSysTotalSize ()

{

Return getTotalSize ("/data ");

}

}

 

 

If you want to write data in the system's storage space, you can use context. getFilesDir (). getAbsolutePath () to obtain the writable directory of the application, that is, the/data/main package name/files directory. data can be written to the application. When the application is uninstalled, also deleted, so you don't have to worry about spam. If not, criticize and correct them.

 

From jacp's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.