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。有什么问题欢迎提出来,我们可以一起改进,一起进步哦。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值