A trickery method to select a location before saving the file to the local phone

Source: Internet
Author: User

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

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.