刚开始觉得简单,不就是找到u盘路径,打开文件写入文件
fun getUsbPath(): String? {
val usbRoot = File("/mnt/media_rw")
if (usbRoot.exists() && usbRoot.isDirectory()) {
val usbFiles = usbRoot.listFiles()
if (usbFiles != null && usbFiles.size > 0) {
return usbFiles[0].absolutePath
}
}
return ""
}
fun copyFileToUsb( sourcePath: String?, usbPath: String?):Int {
val sourceFile = File(sourcePath,DB_NAME)
try {
val uPath = File(usbPath+"/Download")
Log.d("validate", "DataMngActivity usbPath : $usbPath/Download")
if (!uPath.exists()) {
uPath.createNewFile() // 创建文件夹
}
val usbFile = File(usbPath+"/Download/"+DB_NAME)
FileInputStream(sourceFile).use { fis ->
FileOutputStream(usbFile).use { fos ->
val buffer = ByteArray(1024)
var length: Int
while (fis.read(buffer).also { length = it } > 0) {
fos.write(buffer, 0, length)
}
fos.getFD().sync();
return 0;
}
}
} catch (e: IOException) {
e.printStackTrace()
// Handle exception
}
return 1;
}
另外在配上权限啥的,谁知道不插u盘,其他系统文件路径可以,换了u盘报没权限,几次搜索发现android11后有些权限用法不管用了
Runtime.getRuntime().exec("cp 也不行
后来发现一个能行的办法
FireflyApi2.getInstance().execSuCmd("cp "+rootPath_Db+"/"+DB_NAME+" "+f+"/"+DB_NAME);