android 应用mount U盘导出数据

本文介绍如何在Android 5.1设备上,通过获取权限并创建临时目录,利用Runtime执行命令实现数据导出到未挂载的U盘。涉及权限管理、文件操作和挂载/卸载过程。

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

项目开发过程中, 有导出excel文件到u盘的需求, 但是使用的android5.1系统u盘没有挂载, 导致数据无法导出。
解决方式如下:

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
	String folder = "/system/etc/usb_tmp";
        String filePath = folder+"/excel.xls";
        try {
            //创建临时文件夹usb_tmp
            Process su = Runtime.getRuntime().exec("/system/xbin/su");
            String cmd = "mkdir "+folder+"\n" +"chmod 777 "+folder +"\nexit\n";
            Log.d("", "mkdir:usb_tmp \n"+cmd);
            su.getOutputStream().write(cmd.getBytes());
            if(su.waitFor()!=0){
                Log.d("", "mkdir:usb_tmp:  false ");
                flag = false;
            }
            //文件导出到usb_tmp
            File file = new File(filePath);
            OutputStream outputStream= new FileOutputStream(file);
            //自定义类ExportExcel excel文件导出
            ExportExcel.exportExcel(outputStream,list);
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        }
        int count = 0;
        try {
            //u盘插入硬件位置/dev/block/sd*  可以通过adb shell查看
            File dFolder = new File("/dev/block");
            File[] files = dFolder.listFiles();
            for(File file : files){
                if(file.getName().startsWith("sd")){
                    count++;
                    String mountPath = "/system/etc/usb_"+count;
                    //创建临时文件夹mountPath; u盘挂载在mountPath上; 复制excel文件到u盘; umount mountPath; 删除mountPath
                    Process su = Runtime.getRuntime().exec("/system/xbin/su");
                    String cmd = "mkdir "+mountPath+"\n" +"chmod 777 "+mountPath + "\nmount -t vfat "+file.getAbsolutePath()+" "+mountPath+"\ncp "+filePath+" "+mountPath+"\numount "+mountPath+"\nrm -r "+mountPath+"\nexit\n";
                    Log.d("", "mount_copy_umount \n"+cmd);
                    su.getOutputStream().write(cmd.getBytes());
                    if(su.waitFor()!=0){
                        flag = false;
                        Log.d("", "mount_copy_umount: false");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        }
        try {
            //删除usb_tmp
            Process su = Runtime.getRuntime().exec("/system/xbin/su");
            String cmd = "rm -r "+folder+"\nexit\n";
            su.getOutputStream().write(cmd.getBytes());
            if(su.waitFor()!=0){
                Log.d("", "rm -r usb_tmp:  false ");
                flag = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        }
        if(flag && count>0){
            showToast("数据已保存至u盘");
        }else {
            showToast("数据保存至U盘失败,请确认U盘已挂载");
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值