qq:1255295220 希望指导下谢谢!
第一种方式。将已经存在的db,拷贝到sd卡指定目录。如果是包名下路径,不需要权限,非包名下需要申请读写权限
第二种方式。
数据迁移,读取已经存在的数据,迁移到新的数据库中
如果是其他路径
val inputstream= FileInputStream(File("这里指定SD路径(需要申请读写权限)"))
val newFile =
File(cacheDir,"test.db")
val fos = FileOutputStream(newFile)
var len = -1
val buffer = ByteArray(1024)
while (inputstream.read(buffer).also { len = it } != -1) {
fos.write(buffer, 0, len)
}
fos.close()
inputstream.close()
题主参考下下面链接
https://b23.tv/LHvJNxJ
如果是用代码实现,即正常的 文件流复制。
请参考以下工具类,将 源文件
、目标目录
互换下,即可实现复制。
注意:APP需要 申请读写授权
关于Android开发中SD卡使用,复制asset目录的文件到SD卡下工具类
https://blog.csdn.net/djp13276475747/article/details/88617522
数据库转换,希望对你有帮助https://blog.csdn.net/u012729481/article/details/18004869
可以先复制到电脑桌面,然后在你图中红框位置右键,点击Upload,弹窗里选择这个电脑桌面这个db文件,就复制进去了
Java正常的流操作就行,和复制一个图片没区别,操作前一定要在xml中声明对源文件夹和目的文件夹的读写权限
/**
* 复制单个文件
*
* @param oldPath$Name String 原文件路径+文件名 如:data/user/0/com.test/files/abc.txt
* @param newPath$Name String 复制后路径+文件名 如:data/user/0/com.test/cache/abc.txt
* @return <code>true</code> if and only if the file was copied;
* <code>false</code> otherwise
*/
public boolean copyFile(String oldPath$Name, String newPath$Name) {
try {
File oldFile = new File(oldPath$Name);
if (!oldFile.exists()) {
Log.e("--Method--", "copyFile: oldFile not exist.");
return false;
} else if (!oldFile.isFile()) {
Log.e("--Method--", "copyFile: oldFile not file.");
return false;
} else if (!oldFile.canRead()) {
Log.e("--Method--", "copyFile: oldFile cannot read.");
return false;
}
/* 如果不需要打log,可以使用下面的语句
if (!oldFile.exists() || !oldFile.isFile() || !oldFile.canRead()) {
return false;
}
*/
FileInputStream fileInputStream = new FileInputStream(oldPath$Name);
FileOutputStream fileOutputStream = new FileOutputStream(newPath$Name);
byte[] buffer = new byte[1024];
int byteRead;
while (-1 != (byteRead = fileInputStream.read(buffer))) {
fileOutputStream.write(buffer, 0, byteRead);
}
fileInputStream.close();
fileOutputStream.flush();
fileOutputStream.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}