Android仿電腦端選擇文件存儲位置實例

       前段時間寫的圖片選擇框架,今天再補充完善一下,拍照指定路徑的操作以前是我在代碼裏面直接寫好的,不是用戶自己操作選擇存儲在那個文件夾下,今天特地補充一下,後續可能增加選擇本地視頻文件的操作,敬請期待哦偷笑

       首先效果圖奉上:

           接下來分享代碼的實現步驟:

           佈局文件如下:簡單的Toolbar+HorizontalScrollView+ListView組合

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/mis_actionbar_color"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:minHeight="?android:attr/actionBarSize">

    </android.support.v7.widget.Toolbar>
    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:scrollbars="none">
        <LinearLayout
            android:id="@+id/ll_names"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:gravity="center_vertical">

        </LinearLayout>
    </HorizontalScrollView>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/mis_actionbar_color"/>
    <ListView
        android:id="@+id/lv_show"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        android:divider="@color/colorPrimary"
        android:dividerHeight="1dp"></ListView>
</LinearLayout>

            安卓代碼查找手機存儲卡路徑和TF卡個數及路徑的代碼:

/**
 * 獲取外置SD卡路徑以及TF卡的路徑
 * <p>
 * 返回的數據:paths.get(0)肯定是外置SD卡的位置,因爲它是primary external storage.
 *
 * @return 所有可用於存儲的不同的卡的位置,用一個List來保存
 */
public static List<String> getExtSDCardPathList() {
    List<String> paths = new ArrayList<String>();
    String extFileStatus = Environment.getExternalStorageState();
    File extFile = Environment.getExternalStorageDirectory();
    //首先判斷一下外置SD卡的狀態,處於掛載狀態才能獲取的到
    if (extFileStatus.equals(Environment.MEDIA_MOUNTED)
            && extFile.exists() && extFile.isDirectory()
            && extFile.canWrite()) {
        //外置SD卡的路徑
        paths.add(extFile.getAbsolutePath());
    }
    try {
        // obtain executed result of command line code of 'mount', to judge
        // whether tfCard exists by the result
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("mount");
        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        int mountPathIndex = 1;
        while ((line = br.readLine()) != null) {
            // format of sdcard file system: vfat/fuse
            if ((!line.contains("fat") && !line.contains("fuse") && !line
                    .contains("storage"))
                    || line.contains("secure")
                    || line.contains("asec")
                    || line.contains("firmware")
                    || line.contains("shell")
                    || line.contains("obb")
                    || line.contains("legacy") || line.contains("data")) {
                continue;
            }
            String[] parts = line.split(" ");
            int length = parts.length;
            if (mountPathIndex >= length) {
                continue;
            }
            String mountPath = parts[mountPathIndex];
            if (!mountPath.contains("/") || mountPath.contains("data")
                    || mountPath.contains("Data")) {
                continue;
            }
            File mountRoot = new File(mountPath);
            if (!mountRoot.exists() || !mountRoot.isDirectory()
                    || !mountRoot.canWrite()) {
                continue;
            }
            boolean equalsToPrimarySD = mountPath.equals(extFile
                    .getAbsolutePath());
            if (equalsToPrimarySD) {
                continue;
            }
            //擴展存儲卡即TF卡或者SD卡路徑
            paths.add(mountPath);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return paths;
}
            以前的智能手機還都支持外部存儲卡,也就是這裏所說的TF卡,不過隨着現在手機內存的增加越來越多的手機不再支持外部存儲卡功能。

/**
 * 點擊listview的item更新listview的內容方法
 * */
private void scanFile(File file) {
    list.clear();
    if (file.exists()) {
        File files[] = file.listFiles(); // 聲明目錄下所有的文件 files[];
            for (int i = 0; i < files.length; i++) {
                if(files[i].isDirectory()){
                    BaseInfo baseInfo=new BaseInfo();
                    baseInfo.type=0;
                    baseInfo.name=files[i].getAbsolutePath().substring(files[i].getAbsolutePath().lastIndexOf("/")+1,files[i].getAbsolutePath().length());
                    baseInfo.path=files[i].getAbsolutePath();
                    list.add(baseInfo);
                }
        }
    }else {
        Toast.makeText(PathSelectActivity.this,"文件不存在",Toast.LENGTH_SHORT).show();
    }
    lvAdapter.notifyDataSetChanged();
  }

           點擊每個文件以後的思路是遍歷該文件夾下的所有文件,判斷如果是文件夾就加入到listview的數據源中,然後刷新listview顯示。

            最後頂部導航的實現方法時每點擊一次item就在上面加一個view,再次點擊該view就把該view後面的所有view清楚掉,具體實現代碼如下:

positionView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        int count=ll_names.getChildCount();
        int poi=0;
        for (int i = 0; i < count; i++) {
            PositionView positionView1= (PositionView) ll_names.getChildAt(i);
            if(positionView1.getPath().equals(positionView.getPath())){
                scanFile(new File(positionView.getPath()));
                poi=i;
            }
        }
        for (int i = ll_names.getChildCount() - 1; i >poi ; i--) {
            ll_names.removeViewAt(i);
        }
    }
});
          功能涉及到的幾點都在這裏了,具體完整的項目鏈接https://github.com/BoBoAndroid/PicSelectAndShow。有什麼問題歡迎提出來,我們可以一起改進,一起進步哦。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章