1. 程式人生 > >Android 讀取手機資料夾向指定資料夾下存放

Android 讀取手機資料夾向指定資料夾下存放

        昨天專案需要向指定的資料夾下儲存圖片,需要使用檔案管理器去選擇指定的資料夾,當然最後由於邏輯太奇葩(不能選擇資料夾,只能選擇資料夾下的某一個檔案)被否定了,改為自己讀取手機儲存的資料夾,並且可以建立。當中遇到了幾個問題記錄一下:

      1.手機儲存現在由三部分組成了基本上:內部儲存、手機內建外部儲存(ROM)、手機SD卡

         1)內部儲存:

     getFilesDir() 
這個方法可以的到手機內部的儲存(好像是包資料夾下的,我沒測試)

        2)手機內建外部儲存(Rom):

    new File(Environment.getExternalStorageDirectory().getAbsolutePath())
這個方法可以得到檔案

       3)SD卡儲存 問題來了,上面的方法好像似曾相識,不就是得到Sd卡嗎?我也是被困擾了很久,當然也沒得到答案,沒辦法了,通過下面方法得到了:

        File file = new File("/storage/sdcard1");
        if (null != file.listFiles() && file.listFiles().length > 0) {
            fileNameList.add("外接儲存");
        }

更正:如果用以上方法來判斷是否掛在sd卡,以及獲取sd卡的根目錄,測試中有些手機是不準確的,因為有的手機外接sd卡的根路徑不一定是“/storage/sdcard1”,因此找個一個大神的方法,抽取出來他的工具類以及bean類,如下:

  public class StorageUtils {
    public static ArrayList<StorageBean> getStorageData(Context pContext) {
        final StorageManager storageManager = (StorageManager) pContext.getSystemService(Context.STORAGE_SERVICE);
        try {
            //得到StorageManager中的getVolumeList()方法的物件
            final Method getVolumeList = storageManager.getClass().getMethod("getVolumeList");
            //得到StorageVolume類的物件
            final Class<?> storageValumeClazz = Class.forName("android.os.storage.StorageVolume");
            //獲得StorageVolume中的一些方法
            final Method getPath = storageValumeClazz.getMethod("getPath");
            Method isRemovable = storageValumeClazz.getMethod("isRemovable");

            Method mGetState = null;
            //getState 方法是在4.4_r1之後的版本加的,之前版本(含4.4_r1)沒有
            // (http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/os/Environment.java/)
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
                try {
                    mGetState = storageValumeClazz.getMethod("getState");
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }
            //呼叫getVolumeList方法,引數為:“誰”中呼叫這個方法
            final Object invokeVolumeList = getVolumeList.invoke(storageManager);
            final int length = Array.getLength(invokeVolumeList);
            ArrayList<StorageBean> list = new ArrayList<>();
            for (int i = 0; i < length; i++) {
                final Object storageValume = Array.get(invokeVolumeList, i);//得到StorageVolume物件
                final String path = (String) getPath.invoke(storageValume);
                final boolean removable = (Boolean) isRemovable.invoke(storageValume);
                String state;
                if (mGetState != null) {
                    state = (String) mGetState.invoke(storageValume);
                } else {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                        state = Environment.getStorageState(new File(path));
                    } else {
                        if (removable) {
                            state = EnvironmentCompat.getStorageState(new File(path));
                        } else {
                            //不能移除的儲存介質,一直是mounted
                            state = Environment.MEDIA_MOUNTED;
                        }
                    }
                }
                long totalSize = 0;
                long availaleSize = 0;
                if (Environment.MEDIA_MOUNTED.equals(state)) {
                    totalSize = StorageUtils.getTotalSize(path);
                    availaleSize = StorageUtils.getAvailableSize(path);
                }
                StorageBean storageBean = new StorageBean();
                storageBean.setAvailableSize(availaleSize);
                storageBean.setTotalSize(totalSize);
                storageBean.setMounted(state);
                storageBean.setPath(path);
                storageBean.setRemovable(removable);
                list.add(storageBean);
            }
            return list;
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static long getTotalSize(String path) {
        try {
            final StatFs statFs = new StatFs(path);
            long blockSize = 0;
            long blockCountLong = 0;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                blockSize = statFs.getBlockSizeLong();
                blockCountLong = statFs.getBlockCountLong();
            } else {
                blockSize = statFs.getBlockSize();
                blockCountLong = statFs.getBlockCount();
            }
            return blockSize * blockCountLong;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    private static long getAvailableSize(String path) {
        try {
            final StatFs statFs = new StatFs(path);
            long blockSize = 0;
            long availableBlocks = 0;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                blockSize = statFs.getBlockSizeLong();
                availableBlocks = statFs.getAvailableBlocksLong();
            } else {
                blockSize = statFs.getBlockSize();
                availableBlocks = statFs.getAvailableBlocks();
            }
            return availableBlocks * blockSize;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    private static final long A_GB = 1073741824;
    private static final long A_MB = 1048576;
    private static final int A_KB = 1024;

    public static String fmtSpace(long space) {
        if (space <= 0) {
            return "0";
        }
        double gbValue = (double) space / A_GB;
        if (gbValue >= 1) {
            return String.format("%.2fGB", gbValue);
        } else {
            double mbValue = (double) space / A_MB;
            if (mbValue >= 1) {
                return String.format("%.2fMB", mbValue);
            } else {
                final double kbValue = space / A_KB;
                return String.format("%.2fKB", kbValue);
            }
        }
    }
}
public class StorageBean implements Parcelable {
    /**
     * 根路徑
     */
    private String path;
    /**
     *掛在情況 一種是掛在了 mounted  一種是未掛在 removed
     */
    private String mounted;
    /**
     * 是否可以移除,如果不可移除表示內部儲存,可移除代表TF儲存(外掛SD卡)
     */
    private boolean removable;
    /**
     * 總共大小
     */
    private long totalSize;
    /**
     * 可用大小
     */
    private long availableSize;

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getMounted() {
        return mounted;
    }

    public void setMounted(String mounted) {
        this.mounted = mounted;
    }

    public boolean getRemovable() {
        return removable;
    }

    public void setRemovable(boolean removable) {
        this.removable = removable;
    }

    public long getTotalSize() {
        return totalSize;
    }

    public void setTotalSize(long totalSize) {
        this.totalSize = totalSize;
    }

    public long getAvailableSize() {
        return availableSize;
    }

    public void setAvailableSize(long availableSize) {
        this.availableSize = availableSize;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.path);
        dest.writeString(this.mounted);
        dest.writeByte(removable ? (byte) 1 : (byte) 0);
        dest.writeLong(this.totalSize);
        dest.writeLong(this.availableSize);
    }

    public StorageBean() {
    }
    protected StorageBean(Parcel in) {
        this.path = in.readString();
        this.mounted = in.readString();
        this.removable = in.readByte() != 0;
        this.totalSize = in.readLong();
        this.availableSize = in.readLong();
    }

    public static final Parcelable.Creator<StorageBean> CREATOR = new Parcelable.Creator<StorageBean>() {
        @Override
        public StorageBean createFromParcel(Parcel source) {
            return new StorageBean(source);
        }

        @Override
        public StorageBean[] newArray(int size) {
            return new StorageBean[size];
        }
    };

    @Override
    public String toString() {
        return "StorageBean{" +
                "path='" + path + '\'' +
                ", mounted='" + mounted + '\'' +
                ", removable=" + removable +
                ", totalSize=" + totalSize +
                ", availableSize=" + availableSize +
                '}';
    }
}
       以上工具類就可以了,不過發現有的手機讀取出來的集合中居然後8個儲存位置,當然只要取前兩個就可以了(其他的不知道什麼鬼)。

       2. Android6.0系統的手機讀取不到外接儲存下面的檔案和資料夾
   File[] files = file.listFiles();
這個方法中files為空。什麼鬼?後來發現是許可權問題,雖然在功能清單檔案中申請了許可權,但是6.0需要程式碼中對危險許可權進行動態二次申請所以加上如下程式碼:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//6.0手機
                    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
                            ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {//沒有授權許可權
                        ActivityCompat.requestPermissions(this,
                                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
                                MY_PERMISSIONS_REQUEST_READ_CONTACTS_SD);
                    } else {//授權了許可權
                        file = new File("/storage/sdcard1");
                        nextFileList(file);
                    }
                } else {//6.0以下系統
                    file = new File("/storage/sdcard1");
                    nextFileList(file);
                }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {//手機內建外部存貯
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {//授權同意
                    file = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
                    nextFileList(file);
                } else {//授權被拒絕

                }
            }
            break;
            case MY_PERMISSIONS_REQUEST_READ_CONTACTS_SD://sd卡
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {//授權同意
                    file = new File("/storage/sdcard1");
                    nextFileList(file);
                } else {//授權被拒絕

                }
                break;
        }
    }
當然這裡對於使用者拒絕了需要再次請求處理我們沒有處理,就沒有繼續研究。

        3.顯示的資料夾中含有隱藏的資料夾,需要刪除掉,這個其實很簡單,因為隱藏的資料夾都是以“.”開頭的,所以使用如下方法就可以,順便還有資料夾按照字母排序的方法

        for (int num = 0; num < files.length; num++) {
            if (files[num].isDirectory()) {
                String name = files[num].getName();
                if (!name.startsWith(".")) {//排除掉隱藏的資料夾
                    fileNameList.add(name);
                }
            }
        }
        Collections.sort(fileNameList, Collator.getInstance(Locale.ENGLISH));//對資料夾名稱排序

最後把整個demo放到我們資源中,有需要的可以下載。。。。