Android操作sd卡

// 判断sd卡是否存在
        boolean sdCardExist = Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED);
        // 获得sd卡根目录
        if (sdCardExist) {
            File dir = Environment.getExternalStorageDirectory();
            String path = dir.getAbsolutePath() + "/麻城市宅基地资料/";
            ArrayList<String> folder = getFolder(path);
            listParents = listParent(folder);
        }

------------------------

    // List转换为String[]
    public String[] listParent(ArrayList<String> list) {
        int size = list.size();
        String[] array = new String[size];
        for (int i = 0; i < list.size(); i++) {
            array[i] = (String) list.get(i);
        }
        return array;
    }

    // 检查文件夹是否存在
    boolean isFolderExists(String strFolder) {
        File file = new File(strFolder);
        return file.exists();
    }

    // 扫描文件夹目录下的文件夹名字
    public ArrayList<String> getFolder(String path) {
        ArrayList<String> names = new ArrayList<String>();
        File file = new File(path);
        if (file.isDirectory()) {
            File[] array = file.listFiles();
            for (int i = 0; i < array.length; i++) {
                File f = array[i];
                names.add(f.getName());
                System.out.println("文件夹名称" + f.getName());
            }
        }
        return names;

    }

    // 扫描文件夹里面的数据
    public ArrayList<String> folderScan(String path) {
        File file = new File(path);
        ArrayList<String> names = new ArrayList<String>();
        if (file.isDirectory()) {
            File[] array = file.listFiles();

            for (int i = 0; i < array.length; i++) {
                File f = array[i];

                if (f.isFile()) {// FILE TYPE
                    String name = f.getName();
                    if (name.contains(".jpg")) {
                        names.add(name);
                        fileScan(f.getAbsolutePath());
                    }
                } else {// FOLDER TYPE
                    folderScan(f.getAbsolutePath());
                }
            }
        }
        return names;
    }

    // 扫描
    public void fileScan(String file) {
        Uri data = Uri.parse("file://" + file);
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data));
    }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。