Now the memory of the mobile phone easily built-in 8G, 16G, or even 32G of storage space, in fact, is enough. Of course, if the phone is plugged into an external memory card, such as the phone and then plugged into a 32G memory card. The mobile phone capacity is greatly increased. When the app downloads a saved file, like a UC browser, it asks the user to set a default save path. In general, we will first save the file in the external memory advant (if any), if there is no external memory card, to put these file pictures and so on to the built-in storage space, so as to maximize the phone's built-in storage space is not wasted.
So, I have the following one trickery method, the idea is probably this: before downloading the file, use environment to determine if there is an external memory card, if any, save the file to the external memory advant (of course, the more cautious idea is to calculate the memory card of the remaining empty space, If the file is larger than the size of the save operation, the user is prompted for insufficient memory Blablabla. In the same vein, I will not mention that anymore. If you do not have a memory card, save it to a local storage space.
But here's the problem: when you save to local storage, we're not as handy as saving to an external memory card, just a environment.getexternalstoragedirectory (). GetAbsolutePath (). Save to local also need to know this path in advance, and this path depending on the number of mobile phones are/mnt/sdcard1/, some/mnt/sdcard2/and so on. So how trickery? Look directly at the code.
if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {//has a memory card for corresponding operation}else{file[] files = new File ("/mnt/"). Listfiles (); if (Files.length > 0) {String FilePath = null;for (int p=0;p<files.length;p++) {if (Files [P].isdirectory ()) {if (Files[p].getpath (). IndexOf ("SDcard") > 0) {//path name contains ' sdcard ' this keyword StatFs st = new StatFs (files[ P].getpath ()); int blocksize = St.getblocksize (); int blockcount = St.getblockcount (); if ((BlockSize * blockcount) > 0) { FilePath = Files[p].getpath ();}}} if (FilePath! = null) {//Find local stored path, do the appropriate action}}}
The method above can be used to find the saved path before the save operation. Mainly convenient when there is no external memory card, the number of blocks allocated to external storage is 0, and the size is of course 0. The path to the built-in storage (not 0) can be found quickly. Of course, assuming that the built-in storage path name contains the keyword "SDcard", this is the Trickery method, but also enough to deal with the vast majority of mobile phones.
A trickery method to select a location before saving the file to the local phone