Android获取U盘路径并复制文件夹

该文提供了一种在Android系统中使用Java代码获取U盘路径,并将设备目录内容复制到U盘的方法。通过StorageManager服务获取存储卷路径,检查是否为可挂载状态,然后遍历并复制文件及子目录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文介绍如何复制设备目录到外接U盘目录

获取U盘目录

public static List<String> getUsbPaths(Context cxt) {
        List<String> usbPaths = new ArrayList<>();
        try {
            StorageManager srgMgr = (StorageManager) cxt.getSystemService(Context.STORAGE_SERVICE);
            Class<StorageManager> srgMgrClass = StorageManager.class;
            String[] paths = (String[]) srgMgrClass.getMethod("getVolumePaths").invoke(srgMgr);
            for (String path : paths) {
                Object volumeState = srgMgrClass.getMethod("getVolumeState", String.class).invoke(srgMgr, path);
                if (!path.contains("emulated") && Environment.MEDIA_MOUNTED.equals(volumeState)) usbPaths.add(path);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return usbPaths;
    }

复制文件夹

public  boolean copy(String fromFile, String toFile) {
        File[] currentFiles;
        File root = new File(fromFile);
        if (!root.exists()) {
            return false;
        }
        currentFiles = root.listFiles();
        File targetDir = new File(toFile);
        if (!targetDir.exists()) {
            targetDir.mkdirs();
        }
        for (int i = 0; i < currentFiles.length; i++) {
            if (currentFiles[i].isDirectory())
            {
                copy(currentFiles[i].getPath() + "/", toFile+ "/" + currentFiles[i].getName() + "/");

            } else
            {
                CopySdcardFile(currentFiles[i].getPath(), toFile + "/"+ currentFiles[i].getName());
            }
        }

        return true;
    }


    public  boolean CopySdcardFile(String fromFile, String toFile) {
        try {
            InputStream fosfrom = new FileInputStream(fromFile);
            OutputStream fosto = new FileOutputStream(toFile);
            byte bt[] = new byte[1024];
            int c;
            while ((c = fosfrom.read(bt)) > 0) {
                fosto.write(bt, 0, c);
            }
            fosfrom.close();
            fosto.close();
            return true;

        } catch (Exception ex) {
            return false;
        }
    }

复制指定目录到U盘

public File getPath() {
        List<String> pathList = getUsbPaths(MainActivity.this);
        Log.i(TAG, "pathList = " + pathList.size());
        if (pathList.size() > 0) {
            for (String path : pathList) {
	             File usbFile = new File(path);
	             File[] fileList = usbFile.listFiles();
	             for (File file : fileList) {
	                if (file.getName().equals("test")) {
	                    return file;
	                }
	            }
            }
            return null;
        } else {
            return null;
        }
    }
public void pullFile() {
        File f = getPath();
        if (f != null) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    copy(getExternalFilesDir("") + "/test",f.getPath());
                }
            }).start();
        } else {
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值