Android file storage location switch, android file storage

Source: Internet
Author: User

Android file storage location switch, android file storage

Recently, there is a need for the assistant's google satellite map and OpenCycleMap to download offline map data. to switch between the internal storage and external storage space, because the offline tile data is very large, many outdoor users want to store these files on an external TF card without occupying the internal storage space. So I have sorted out my recent research and shared it with you.

Problems that need to be considered and encountered (mainly the compatibility of different mobile phones and Systems ):

  1. How can I obtain all the mounted storage devices of my mobile phone?

Android does not provide explicit interfaces. First, you must read the source code of the system setting "Storage" of the application to see how the storage is obtained. Finally, find the two important classes StorageManager and StorageVolume, and obtain the StorageVolume [] list through reflection.

  2. What is the uniqueness of a storage?

Storage path? No (some mobile phones do not have a TF card. The internal storage path is/storage/sdcard0. After the TF card is inserted, the internal storage path is/storage/sdcard1, And the TF card is/storage/sdcard0 ).

Memory Card Name? No (the system language may be switched, resulting in name matching failure, and the name resId may not work. In a lower system version, StorageVolume does not have the mDescriptionId attribute ).

After testing, it is found that the mStorageId can indicate the uniqueness of the memory, the number of Memory changes, and the id of each memory will not change.

  3. How to get the name of the storage?

According to the test, there are three main methods for obtaining memory names for different mobile phones: getDescription (), getDescription (Context context), getDescriptionId (), and resId.

  4. What should I do if I switch to the file storage when I download half of the task file?

There are two solutions:

4.1 when switching, if the new bucket has enough files to be transferred, stop all download tasks and copy all downloaded and downloaded files to the new bucket, then, update the storage path of the download database task and resume the download task;

4.2 When switching, copy all downloaded files to the new bucket, download the task, and then move the downloaded files to the new bucket.

  5. Third-party applications cannot read external memory cards on the 4.4 system. (Refer to"External Storage")

In order to completely clean up all the data of the program when the program is uninstalled, google will not be able to write files to the Level 2 storage area.

"WRITE_EXTERNAL_STORAGEPermission must only grant write access to the primary external storage on a device. apps must not be allowed to write to secondary external storage devices, memory T in their package-specific directories as allowed by synthesized permissions. restricting writes in this way ensures the system can clean up files when applications are uninstalled."

To be able to write files to the TF card on the 4.4 system, you must first root. The specific method can be google.

Therefore, on the 4.4 system, switching will lead to file transfer and download failure. If you want to switch to the TF card, at least remind the user and give the root solution on the 4.4 system.

 

Some code for obtaining the memory is as follows:

  

1 public static class MyStorageVolume {2 public int mStorageId; 3 public String mPath; 4 public String mDescription; 5 public boolean mPrimary; 6 public boolean mRemovable; 7 public boolean mEmulated; 8 public int mMtpReserveSpace; 9 public boolean mAllowMassStorage; 10 public long mMaxFileSize; // maximum file size. (0 indicates unlimited) 11 public String mState; // return null 12 13 public MyStorageVolume (Context context, Object reflectItem) {14 try {15 Method fmStorageId = reflectItem. getClass (). getDeclaredMethod ("getStorageId"); 16 fmStorageId. setAccessible (true); 17 mStorageId = (Integer) fmStorageId. invoke (reflectItem); 18} catch (Exception e) {19} 20 21 try {22 Method fmPath = reflectItem. getClass (). getDeclaredMethod (" GetPath "); 23 fmPath. setAccessible (true); 24 mPath = (String) fmPath. invoke (reflectItem); 25} catch (Exception e) {26} 27 28 try {29 Method fmDescriptionId = reflectItem. getClass (). getDeclaredMethod ("getDescription"); 30 fmDescriptionId. setAccessible (true); 31 mDescription = (String) fmDescriptionId. invoke (reflectItem); 32} catch (Exception e) {33} 34 if (mDescription = null | TextUtils. is Empty (mDescription) {35 try {36 Method fmDescriptionId = reflectItem. getClass (). getDeclaredMethod ("getDescription"); 37 fmDescriptionId. setAccessible (true); 38 mDescription = (String) fmDescriptionId. invoke (reflectItem, context); 39} catch (Exception e) {40} 41} 42 if (mDescription = null | TextUtils. isEmpty (mDescription) {43 try {44 Method fmDescriptionId = reflectItem. getClass (). getDecl AredMethod ("getDescriptionId"); 45 fmDescriptionId. setAccessible (true); 46 int mDescriptionId = (Integer) fmDescriptionId. invoke (reflectItem); 47 if (mDescriptionId! = 0) {48 mDescription = context. getResources (). getString (mDescriptionId); 49} 50} catch (Exception e) {51} 52} 53 54 try {55 Method fmPrimary = reflectItem. getClass (). getDeclaredMethod ("isPrimary"); 56 fmPrimary. setAccessible (true); 57 mPrimary = (Boolean) fmPrimary. invoke (reflectItem); 58} catch (Exception e) {59} 60 61 try {62 Method fisRemovable = reflectItem. getClass (). getDeclaredMethod ("isRemovable"); 63 fisRemovable. setAccessible (true); 64 mRemovable = (Boolean) fisRemovable. invoke (reflectItem); 65} catch (Exception e) {66} 67 68 try {69 Method fisEmulated = reflectItem. getClass (). getDeclaredMethod ("isEmulated"); 70 fisEmulated. setAccessible (true); 71 mEmulated = (Boolean) fisEmulated. invoke (reflectItem); 72} catch (Exception e) {73} 74 75 try {76 Method fmMtpReserveSpace = reflectItem. getClass (). getDeclaredMethod ("getMtpReserveSpace"); 77 fmMtpReserveSpace. setAccessible (true); 78 mMtpReserveSpace = (Integer) fmMtpReserveSpace. invoke (reflectItem); 79} catch (Exception e) {80} 81 82 try {83 Method fAllowMassStorage = reflectItem. getClass (). getDeclaredMethod ("allowMassStorage"); 84 fAllowMassStorage. setAccessible (true); 85 mAllowMassStorage = (Boolean) fAllowMassStorage. invoke (reflectItem); 86} catch (Exception e) {87} 88 89 try {90 Method fMaxFileSize = reflectItem. getClass (). getDeclaredMethod ("getMaxFileSize"); 91 fMaxFileSize. setAccessible (true); 92 mMaxFileSize = (Long) fMaxFileSize. invoke (reflectItem); 93} catch (Exception e) {94} 95 96 try {97 Method fState = reflectItem. getClass (). getDeclaredMethod ("getState"); 98 fState. setAccessible (true); 99 mState = (String) fState. invoke (reflectItem); 100} catch (Exception e) {101} 102 103/** 104 * Get the Volume mount status, such as Environment. MEDIA_MOUNTED106 */107 public String getVolumeState (Context context) {108 return StorageVolumeUtil. getVolumeState (context, mPath); 109} 110 111 public boolean isMounted (Context context) {112 return getVolumeState (context ). equals (Environment. MEDIA_MOUNTED); 113} 114 115 public String getDescription () {116 return mDescription; 117} 118 119/** 120 * obtain the unique identifier of the storage device 121 */122 public String getUniqueFlag () {123 return "" + mStorageId; 124} 125 126/* public boolean isUsbStorage () {127 return mDescriptionId = android. r. string. storage_usb; 128} */129 130/** 131 * Get the available Directory space size 132 */133 public long getAvailableSize () {134 return StorageVolumeUtil. getAvailableSize (mPath); 135} 136 137/** 138 * Get the total storage space of the Directory 139 */140 public long getTotalSize () {141 return StorageVolumeUtil. getTotalSize (mPath); 142} 143 144 @ Override145 public String toString () {146 return "MyStorageVolume {" + 147 "\ nmStorageId =" + mStorageId + 148 "\ n, mPath = '"+ mPath +' \'' + 149 "\ n, mDescription =" + mDescription + 150 "\ n, mPrimary =" + mPrimary + 151 "\ n, mRemovable = "+ mRemovable + 152" \ n, mEmulated = "+ mEmulated + 153" \ n, mMtpReserveSpace = "+ mMtpReserveSpace + 154" \ n, mAllowMassStorage = "+ mAllowMassStorage + 155" \ n, mMaxFileSize = "+ mMaxFileSize + 156" \ n, mState = '"+ mState +' \'' + 100'} '+ "\ n"; 157} 158}Storage information MyStorageVolume

 

1 public static List <MyStorageVolume> getVolumeList (Context context) {2 List <MyStorageVolume> svList = new ArrayList <rule> (3); 3 StorageManager mStorageManager = (StorageManager) context 4. getSystemService (Activity. STORAGE_SERVICE); 5 try {6 Method Using hodgetpaths = mStorageManager. getClass (). getMethod ("getVolumeList"); 7 Object [] list = (Object []) using hodgetpaths. invoke (mStorageManager); 8 for (Object item: list) {9 svList. add (new MyStorageVolume (context, item); 10} 11} catch (Exception e) {12 e. printStackTrace (); 13} 14 return svList; 15}Retrieve all memory

 

Test example on github:

Https://github.com/John-Chen/BlogSamples/tree/master/StorageTest

  

If you have nothing to consider, please feel free to discuss it.

 

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.