訪問SD卡 所需權限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
App專屬文件 vs App獨立文件
app專屬文件就是那些只有該app才可以使用的文件,例如專屬格式的電子書,app獨立文件就是那些不依賴於某特定app的文件,例如照片。
App獨立文件
這類文件當我們刪除應用之后,還應該保留在手機上的,例如拍照的照片,不應該隨着刪除應用而被刪除掉。對於這類文件,android給我們提供了特定的目錄,這些目錄都是以DIRECTORY開頭的,例如:DIRECTORY_MUSIC , DIRECTORY_PICTURES.
訪問這些文件夾有兩種方式:
Environment.getExternalStorageDirectory()是獲得外部存儲的第一層的對象
第一種:File sdCard = Environment.getExternalStorageDirectory();
這個sdCard的路徑為mnt/sdcard/ 即為SD卡根路徑,我們可以指定訪問的文件夾名
File sdCard = Environment.getExternalStorageDirectory();
File directory_pictures = new File(sdCard, "Pictures");
Log.i(TAG,"directory_pictures="+directory_pictures);
得到的路徑如下:
第二種:Environment.getExternalStoragePublicDirectory(String type)
如果您需要往sdcard中保存特定類型的內容,可以考慮使用Environment.getExternalStoragePublicDirectory(String type)函數,該函數可以返回特定類型的目錄,目前支持如下類型:
DIRECTORY_ALARMS //警報的鈴聲
DIRECTORY_DCIM //相機拍攝的圖片和視頻保存的位置
DIRECTORY_DOWNLOADS //下載文件保存的位置
DIRECTORY_MOVIES //電影保存的位置, 比如 通過google play下載的電影
DIRECTORY_MUSIC //音樂保存的位置
DIRECTORY_NOTIFICATIONS //通知音保存的位置
DIRECTORY_PICTURES //下載的圖片保存的位置
DIRECTORY_PODCASTS //用於保存podcast(博客)的音頻文件
DIRECTORY_RINGTONES //保存鈴聲的位置
File directory_pictures = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
Log.e(TAG, "directory_pictures="+directory_pictures);
得到的路徑如下:
第二種方法是一個更加方便的訪問Android給我們提供好的一些公共目錄的方法,第一種方式更加靈活,可以自己指定目錄。
===================================================================
Android系統提供了Environment.getExternalStorageDirectory()接口獲得存儲設備的路徑,但是這個接口往往給出的結果並不是我們想要的,在某些設備上它返回的是手機內部存儲,某些設備上返回的手機外部存儲。還有就是某些android設備支持擴展多個sdcard,這個時候想要獲得所有存儲器的掛載路徑,這個接口是沒有辦法辦到的。
那么,Android系統的文件管理器是如何把所有掛載的存儲設備加載出來的呢?通過查看文件管理器的源碼發現是在MountPointManager類中處理的,通過調用StorageManager類的getVolumeList()方法獲取的。
- /**
- * This method initializes MountPointManager.
- *
- * @param context Context to use
- */
- public void init(Context context) {
- mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
- final String defaultPath = getDefaultPath();
- LogUtils.d(TAG, "init,defaultPath = " + defaultPath);
- if (!TextUtils.isEmpty(defaultPath)) {
- mRootPath = ROOT_PATH;
- }
- mMountPathList.clear();
- // check media availability to init mMountPathList
- StorageVolume[] storageVolumeList = mStorageManager.getVolumeList();
- if (storageVolumeList != null) {
- for (StorageVolume volume : storageVolumeList) {
- MountPoint mountPoint = new MountPoint();
- mountPoint.mDescription = volume.getDescription(context);
- mountPoint.mPath = volume.getPath();
- mountPoint.mIsMounted = isMounted(volume.getPath());
- mountPoint.mIsExternal = volume.isRemovable();
- mountPoint.mMaxFileSize = volume.getMaxFileSize();
- LogUtils.d(TAG, "init,description :" + mountPoint.mDescription + ",path : "
- + mountPoint.mPath + ",isMounted : " + mountPoint.mIsMounted
- + ",isExternal : " + mountPoint.mIsExternal + ", mMaxFileSize: " + mountPoint.mMaxFileSize);
- mMountPathList.add(mountPoint);
- }
- }
- IconManager.getInstance().init(context, defaultPath + SEPARATOR);
- }
系統提供了StorageManager類,它有一個方法叫getVolumeList(),這個方法的返回值是一個StorageVolume數組,StorageVolume類中封裝了掛載路徑,掛載狀態,以及是否可以移除等信息。下面是這個方法的源碼。
- /**
- * Returns list of all mountable volumes.
- * @hide
- */
- public StorageVolume[] getVolumeList() {
- if (mMountService == null) return new StorageVolume[0];
- try {
- Parcelable[] list = mMountService.getVolumeList();
- if (list == null) return new StorageVolume[0];
- int length = list.length;
- StorageVolume[] result = new StorageVolume[length];
- for (int i = 0; i < length; i++) {
- result[i] = (StorageVolume)list[i];
- }
- return result;
- } catch (RemoteException e) {
- Log.e(TAG, "Failed to get volume list", e);
- return null;
- }
- }
getVolumeList()方法是隱藏的,不能在應用代碼中直接調用,所以我們只能通過反射來調用這個方法了。
通過反射機制獲取Android設備的所有存儲設備
- public class StorageInfo {
- public String path;
- public String state;
- public boolean isRemoveable;
- public StorageInfo(String path) {
- this.path = path;
- }
- public boolean isMounted() {
- return "mounted".equals(state);
- }
- @Override
- public String toString() {
- return "StorageInfo [path=" + path + ", state=" + state
- + ", isRemoveable=" + isRemoveable + "]";
- }
- }
- public static List<StorageInfo> listAllStorage(Context context) {
- ArrayList<StorageInfo> storages = new ArrayList<StorageInfo>();
- StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
- try {
- Class<?>[] paramClasses = {};
- Method getVolumeList = StorageManager.class.getMethod("getVolumeList", paramClasses);
- Object[] params = {};
- Object[] invokes = (Object[]) getVolumeList.invoke(storageManager, params);
- if (invokes != null) {
- StorageInfo info = null;
- for (int i = 0; i < invokes.length; i++) {
- Object obj = invokes[i];
- Method getPath = obj.getClass().getMethod("getPath", new Class[0]);
- String path = (String) getPath.invoke(obj, new Object[0]);
- info = new StorageInfo(path);
- Method getVolumeState = StorageManager.class.getMethod("getVolumeState", String.class);
- String state = (String) getVolumeState.invoke(storageManager, info.path);
- info.state = state;
- Method isRemovable = obj.getClass().getMethod("isRemovable", new Class[0]);
- info.isRemoveable = ((Boolean) isRemovable.invoke(obj, new Object[0])).booleanValue();
- storages.add(info);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- storages.trimToSize();
- return storages;
- }
- public static List<StorageInfo> getAvaliableStorage(List<StorageInfo> infos){
- List<StorageInfo> storages = new ArrayList<StorageInfo>();
- for(StorageInfo info : infos){
- File file = new File(info.path);
- if ((file.exists()) && (file.isDirectory()) && (file.canWrite())) {
- if (info.isMounted()) {
- storages.add(info);
- }
- }
- }
- return storages;
- }
調用上述方法:
- List<StorageInfo> list = listAllStorage(this);
- for(StorageInfo info : list){
- Log.e(TAG, info.toString());
- }
- Log.e(TAG, "-----------------");
- List<StorageInfo> infos = getAvaliableStorage(list);
- for(StorageInfo info : infos){
- Log.e(TAG, info.toString());
- }
- Log.e(TAG, "Environment.getExternalStorageDirectory(): " + Environment.getExternalStorageDirectory());
連上手機進行驗證,輸出Log信息:
可以看到,通過listAllStorage()方法獲取到了手機上的所有存儲設備,通過getAvaliableStorage()方法的過濾獲取到了掛載狀態的所有存儲設備。由於該手機只有一個可讀寫的存儲設備,因此與Environment.getExternalStorageDirectory()方法獲取到的結果一致。
===================================================================
getExternalFilesDir(null)參數傳入的為null,這樣默認訪問的是files文件夾,我們可以指定子文件夾
getExternalFilesDir(null) 得到 "
”/mmn/sdcard/Android/data/< package name >/files/
getExternalFilesDir("Caches") 得到 "
/mmn/sdcard/Android/data/< package name >/files/Caches
"
有些時候我們的手機沒有安裝SD卡,所以我們使用前需要判斷一下:
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { //SD卡已裝入 }