Method for Android to obtain SD card path and SD card memory

This paper describes the method of Android to obtain SD card path and SD card memory. Share with you for your reference. The specific analysis is as follows:

Yesterday, after studying the problem of storage path broken through after taking photos, the storage path began to be written as: private string folder = "/ sdcard / DCIM / camera /" (image storage path of photo taking program on SD card); Later, it was found that although there is generally no error in this writing, it is not very good, because different cameras may have problems with the path. A better way is to obtain the path through the environment. Finally, an example is given to teach you how to obtain the memory of the sdcard and display it to tell the user. The contents are as follows:

0. Obtain the SD card path. 1. Describes the environment class. 2. Describes the statfs class. 3. The complete example reads the sdcard memory

0. Get SD card path

Method 1:

Method 2:

Then: add a slash after the file name

1. About the environment class

Environment is a class that provides access to environment variables. Environment contains constants:

MEDIA_ BAD_ Remove explanation: getexternalstoragestate() is returned, indicating that the sdcard has been removed before it is unloaded_ Checking explanation: getexternalstoragestate() is returned, indicating that the object is checking the disk. MEDIA_ Mounted explanation: getexternalstoragestate() is returned, indicating whether the object exists and has read / write permission media_ MOUNTED_ READ_ Only interpretation: getexternalstoragestate() is returned, indicating that the object permission is read-only media_ Nofs explanation: getexternalstoragestate() is returned, indicating that the object is blank or using an unsupported file system. MEDIA_ Removed interpretation: getexternalstoragestate() is returned. If sdcard does not exist, media is returned_ Shared explanation: return getexternalstoragestate(). If the sdcard is not installed, return to media via USB mass storage share_ UNMOUNTABLE explanation: getexternalstoragestate() is returned. The returned sdcard cannot be installed. If the sdcard exists but cannot be installed, media_ Unmounted explanation: return getexternalstoragestate() and return that the sdcard has been removed. If the sdcard exists but is not installed

Common methods of environment:

Method: getdatadirectory() interpretation: return file to get Android data directory. Method: getdownloadcachedirectory() explanation: return file to get the Android download / cache content directory. Method: getexternalstoragedirectory() interpretation: return file to get the external storage directory, i.e. sdcard method: getexternalstoragepublicdirectory (string type) interpretation: return file to get a high-end public external storage directory to place some types of files method: getexternalstoragestate() interpretation: return file, Get the current status of external storage device method: getrootdirectory() explanation: return file to get the root directory of Android 2. Tell about statfs class statfs, a class that simulates the DF command of Linux, and get the usage of SD card and mobile phone memory. Common methods of statfs:

Getavailableblocks() explanation: return int to get the currently available storage space getblockcount() explanation: return int to get the number of file systems available in the area getblocksize() explanation: return int, size, in bytes, a file system getfreeblocks() explanation: return int, the remaining space in the block area restat (string path) Explanation: execute a file system referenced by the object. 3. Read the complete example. The sdcard memory card can be plugged in and out at any time on the Android phone. Each action will act on the operating system_ Broadcast, this example will use the methods learned above to calculate the remaining capacity and total capacity of the sdcard. The code is as follows:

I hope this article will be helpful to your Android program design.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>