在访问SD卡根目录并创建备份之前需要搜索一会儿。我也尝试使用
String rawSecondaryStoragesStr = System.getenv("SECONDARY_STORAGE");
并返回正确的路径storage/sdcard/sdcard1
。并尝试使用
directory
try {
File f = new File(System.getenv("SECONDARY_STORAGE"), "ATTS");
if (!f.exists()) {
boolean isDirectoryCreated = f.exists();
if (!isDirectoryCreated) {
isDirectoryCreated = f.mkdir();
Log.e("success!", "HEY!!!");
}
} else {
Log.e("12", "12");
}
} catch (Throwable e) {
e.printStackTrace();
}
,并在检查日志后,记录“成功”。但是在检查设备浏览器后,不会创建该文件夹。这是为什么?。
这是我对Manifest.xml的许可
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="22"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="22"/>
我还可以问用户是否需要接受5.1上的运行时权限。因为在4.4或更低版本上。它会自动接受,无需用户干预。谢谢!
编辑:添加数据库备份代码
private void DBBackup() {
try {
File data = Environment.getDataDirectory();
File sd = new File("/storage/sdcard1/ATTS");
if (sd.canWrite()) {
String currentDBPath = "/data/com.jti.mikee.jti_pos/databases/" + DBHelper.DB_NAME;
String backupDBPath = DBHelper.DB_NAME;
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}