Get parameters for internal storage and external storage space on your Android phone && how to determine where an APK is installed

Source: Internet
Author: User

Ext.: http://blog.csdn.net/zhandoushi1982/article/details/8560233

Obtaining information about the Android file system requires support for the environment class and the Statfs class.

(1) environment is a class that provides access to environment variables, and the common methods are:

A,getrootdirectory (), returns file, gets the root directory of Android.

B,getdatadirectory (), returns file, gets the Android data directory.

C,getexternalstoragedirectory (), returns file, gets the external storage directory, which is sdcard.
D,getexternalstoragestate (), returns a string that gets the current status string for the external storage device.

E,getdownloadcachedirectory (), returns file, gets the android download/cache content directory.

Commonly used string variables, take the getexternalstoragestate return value as an example:

A,media_bad_removal, indicating that the sdcard was removed before it was unloaded.
B,media_checking that indicates that the object is being checked for disk.
C,media_mounted, indicates that the object exists and has read/write permissions.

D,media_mounted_read_only, indicating that the object permission is read-only.

E,media_nofs, indicates that the object is blank or is using an unsupported file system.

F,media_removed, there is no sdcard.

G,media_shared, indicates that the storage media is being shared via USB, such as when the phone is connected to the phone via a data cable, and the app cannot get the data on the storage media.
H,media_unmountable, indicating that the storage media could not be mounted.

I, media_unmounted, indicates that the storage media is not mounted.

(2) Statfs class, a class that simulates the DF command of Linux, the member function is a property function that obtains the underlying Linux file system. StatFs Common methods:

A,getavailableblocks (), returns an int that gets the number of blocks of available storage space for the current system.

B,getblockcount (), returns an int that gets the total number of blocks in the current system storage space.

C,getblocksize (), returns int, gets the size of the current system block, in bytes

D,getfreeblocks (), which returns int, the space remaining in the block area (including free space and reserved space).
(3) An instance of acquiring the available capacity and total amount of the system and SD card, as follows:

Importandroid.os.Environment;ImportAndroid.os.StatFs;ImportJava.io.File; Public Longgetavailableinternalmemorysize () {File path=environment.getdatadirectory (); StatFs Stat=NewStatFs (Path.getpath ()); LongBlockSize =stat.getblocksize (); LongAvailableblocks =stat.getavailableblocks (); returnavailableblocks*BlockSize;}  Public Longgettotalinternalmemorysize () {File path=environment.getdatadirectory (); StatFs Stat=NewStatFs (Path.getpath ()); LongBlockSize =stat.getblocksize (); LongTotalblocks =Stat.getblockcount (); returntotalblocks*BlockSize;}  Public Booleanexternalmemoryavailable () {returnenvironment.getexternalstoragestate (). Equals (environment.media_mounted);}  Public Longgetavailableexternalmemorysize () {if(Externalmemoryavailable ()) {File path=environment.getexternalstoragedirectory (); StatFs Stat=NewStatFs (Path.getpath ()); LongBlockSize =stat.getblocksize (); LongAvailableblocks =stat.getavailableblocks (); returnavailableblocks*blockSize; }        Else{            return-1; }}     Public Longgettotalexternalmemorysize () {if(Externalmemoryavailable ()) {File path=environment.getexternalstoragedirectory (); StatFs Stat=NewStatFs (Path.getpath ()); LongBlockSize =stat.getblocksize (); LongTotalblocks =Stat.getblockcount (); returntotalblocks*blockSize; }        Else{            return-1; }}

The data can be output from log, in M units.

        LOG.I ("Zhangcheng", "internal available storage space is:" +long.tostring (Getavailableinternalmemorysize ()/(1024*1024));        LOG.I ("Zhangcheng", "internal total storage space is:" +long.tostring (Gettotalinternalmemorysize ()/(1024*1024));                LOG.I ("Zhangcheng", "externally available storage space is:" +long.tostring (Getavailableexternalmemorysize ()/(1024*1024));        LOG.I ("Zhangcheng", "external total storage space is:" +long.tostring (Gettotalexternalmemorysize ()/(1024*1024));  

(4) It is important to note that access to the memory file system also requires the addition of permissions in the XML, as follows:

    <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>    <uses-permission Android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>    <uses-permission android:name= " Android.permission.READ_EXTERNAL_STORAGE "/>

==========================================================================================================

Some applications can be installed on the SD card, some can not, there are some rules. How to judge?

Google's default Packagemanager Management installation package is as follows: There are four possible scenarios for defining the installation location in Adroidmanifest.xml:
1. If the installation location is not defined, it is installed on the phone memory;
2. android:installlocation = "Auto", indicating whether the phone memory is sufficient, if enough is installed on the phone memory, not enough to install on the T card;
3. android:installlocation = "internalonly", which means the device is installed on the phone memory;
4. android:installlocation = "preferexternal", means installed on the T card;

If the apk androidmanifest.xml inside the definition android:installlocation = "internalonly", then no matter where the user chooses to install, the APK will be installed on the phone, The benefit of this is to avoid program run errors, because the apk that defines android:installlocation = "internalonly" is typically installed on the phone's memory to function properly.

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.