SD card enabling and disabling hardware device monitoring process four - SD card disabling and enabling

Android phone has its own storage management system: almost any type of storage device is called "external storage", including internal NAND flash memory, this is called "primary storage", and other Everything is "secondary storage", like an SD card.
With the continuous upgrading of the Android system, the access control of third-party applications to storage has been adjusted many times. The third is complicated. Here we focus on the earliest and most commonly used write request permission "WRITE_EXTERNAL_STORAGE", which allows Apps write to primary storage (built-in flash memory), but not to secondary storage (SD card).






Before Android 4.0, Android devices were single-storage, and third-party apps must apply for the WRITE_EXTERNAL_STORAGE permission to write files.


After 4.0, Android devices began to have built-in flash memory, that is, primary storage, and an external SD card, that is, secondary external storage device. , you must apply for the WRITE_EXTERNAL_STORAGE permission,
which is only used to authorize users to write primary external storage, that is, the system default sd card sdcard0.
And the created files will be freely accessed in the root directory of the application, namely Android/data/package name/folder, and writing is not allowed anywhere else.
When the app is uninstalled, it will also be cleared.
SDCard/SDCard/Android/data/{package_name}/files/ can be obtained through the Context.getExternalFilesDir() method to store some long-term saved data;


SDCard/SDCard/Android/data/{package_name}/cache/ can be obtained through the Context.getExternalCacheDir() method to store temporary cache data;


these two directories correspond to "Clear Data" in Settings->Application->Application Details with the "Clear Cache" option.




WRITE_MEDIA_STORAGE permission to control the operation of secondary external storage device.


As of Android 4.4 KitKat, the WRITE_MEDIA_STORAGE permission is only available to system apps, and is no longer granted to third-party apps.


Fortunately, in Android 5.0 and later, there is a method on the official website to operate the external storage SD card, which can be operated by popping up a dialog box and asking the user to authorize it.

Select ----> SELECT "SD card"





Google solves this problem after making this restriction:


any App will create a directory on /sdcard and /sdcard1, and if it is deleted, it will be rebuilt, even if it is uninstalled, Some junk files will be left behind.


However, a problem also arises:


Similar to video and image processing, if you want to cache a large number of audio and video files in external storage, and you want to keep the App after uninstalling, there is no way.




The fourth monitoring processing of hardware devices - the disabling and enabling of SD card


Idea: It is similar to the disabling and enabling of USB devices, but the registry path of the relationship is different. The principle is exactly the same disabling: SD card is a storage device. Write the key item MountHidden= 1 in "System//StorageManager//Profiles//SDMemory";, and dismount the SD card to enable: Write the key item MountHidden= in "System//StorageManager//Profiles//SDMemory"; 0, mount SD card and re-insert SD card to enable







Original address:

https://my.oschina.net/liucundong/blog/314520

https://plus.google.com/+TodLiebeck/posts/gjnmuaDM8snhttp://blog.csdn.net/olevin/article/details/29575127http://source.android.com/devices/tech/storage/index.html 

https://blog.csdn.net/huaerge/article/details/6454244

https://www.2cto.com/kf/201608/539094.html

https://metactrl.com/docs/sdcard-on-lollipop/



Modify the file platform.xml after rooting the phone

https://technofaq.org/posts/2014/04/fixing-external-sd-card-write-issue-on-android-kitkat/


Get the size of phone memory and built-in and external sdcard

https://blog.csdn.net/qq_28261343/article/details/51176110


Relationship between StorageManager and MountService

https://www.2cto.com/kf/201310/253644.html



Get all storage devices of Android

https://blog.csdn.net/wangsf1112/article/details/51427007


Understanding of Internal Storage and External Storage

https://www.cnblogs.com/wzxwhd/p/5966737.html


Android 8.0 writes to sdcard (specify a specific folder for authorization)

https://www.2cto.com/kf/201802/720060.html


Android 4.4 third-party apps cannot write to external memory cards, but the built-in file manager can

https://www.zhihu.com/question/24639739/answer/131250190



The difference between Environment.getExternalStorageState() and getExternalFileDir()

https://www.journaldev.com/9400/android-external-storage-read-write-save-file

https://www.cnblogs.com/dazhao/p/6547811.html

https://www.jianshu.com/p/a0870499d736



How to operate without external storage, the path for normal development of the application

https://blog.csdn.net/losefrank/article/details/53464646




2. Several concepts or points to pay attention to

①All Android devices have external storage and internal storage. These two names are derived from the early Android devices. At that time, the internal storage of the device was indeed fixed, and the external storage was indeed movable like a U disk. However, in later devices, many mid-to-high-end machines have expanded their body storage to more than 8G, and they conceptually divided the storage into two parts, "internal" and "external", but they are actually inside the phone. So no matter if android phones have removable sdcard or not, they always have external storage and internal storage. The most important thing is that we all use the same api to access the removable sdcard or the storage that comes with the phone (external storage).

Although external storage is a bit complicated in concept, it is also easy to distinguish. When you connect your phone to a computer, the part that can be recognized by the computer must be external storage.

②If your api version is lower than 8, then you can't use getExternalStoragePublicDirectory( ) , but use Environment.getExternalStorageDirectory(), which takes no parameters, so you can't create a directory yourself, just return the root path of external storage.

The way to create an application private file is Context.getExternalFilesDir()

All private files for external storage of applications are placed under Android/data/ in the root directory, in the form of /Android/data/<package_name>/

If your api version is lower than 8, you cannot use getExternalFilesDir()it. Instead, use Environment.getExternalStorageDirectory() to get the root path, and then find a way to manipulate the files under /Android/data/<package_name>/.

④ Save the file in external storage

Since external storage may not be available—for example, when the user has mounted storage to a computer or removed an SD card that provides external storage—you should always confirm its capacity before accessing it. You can query the external storage state by calling getExternalStorageState(). If the returned status is MEDIA_MOUNTED, then you can read and write to your file.

⑤ Since the user mounts the external storage as a USB storage and is not available, it is necessary to check whether it is available and not read-only before using it.

⑥Environment.getExternalStorageState(): Get the path of the internal storage sd card such as: /mnt/sdcard

getExternalFileDir() returns the path of the root directory file of the application in the internal storage sd card, such as: /Android/data/data/application_package/

getExternalFileDir()

getExternalFilesDir()
It returns the path to files folder inside Android/data/data/your_package/ on your SD card. It is used to store any required files for your app (eg images downloaded from web or cache files). Once the app is uninstalled, any data stored in this folder is gone too.
Get the storage directory under the application, /data/data/your_package/, the files stored with the uninstallation of the application are deleted


getExternalStorageDirectory()
It returns the root path to your SD card (eg mnt/sdcard/). If you save data on this path and uninstall the app, that data won't be lost.
Get the root directory of the sd card, with the application Uninstall is irrelevant.


<cache-path>, indicates the default cache root directory of the application, the corresponding root directory is equivalent to getCacheDir(), check the full path: /data/user/0/cn.teachcourse.demos/cache


<external-path>, indicates the external memory Card root directory, the corresponding root directory is equivalent to
Environment.getExternalStorageDirectory(),
check the full path: /storage/emulated/0


<external-files-path>, which means the APP public directory under the root directory of the external memory card, corresponding to the root directory, etc. Equivalent to
Context#getExternalFilesDir(String) Context.getExternalFilesDir(null),
see the full path:
/storage/emulated/0/Android/data/cn.teachcourse.demos/files/Download


<external-cache-path>, which means external memory The APP cache directory in the root directory of the card, the corresponding root directory is equivalent to Context.getExternalCacheDir(), check the full path:
/storage/emulated/0/Android/data/cn.teachcourse.demos/cache


 
 
context.getFilesDir() gets the path of the built-in memory card such as: /data/user/0/com.example.applications/files/
context.getExternalFilesDir() gets the path of the external memory card such as: /storage/emulated/0/Android/data/com.example.applications/files/myinfos
context.getExternalStorageDirectory() gets the root path of the external memory card. After the application is deleted, the content will not be deleted together

 
 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324402458&siteId=291194637