gpt4 book ai didi

java - 由于 mkdirs(),我无法在外部目录中备份领域数据库

转载 作者:行者123 更新时间:2023-12-02 13:08:18 24 4
gpt4 key购买 nike

我正在尝试导出外部存储中的领域数据库(实际上是在我的手机中)

当我按下下面的按钮时,名为 backupRealmNow() 的方法起作用。

public void backupRealmNow() {

Realm nowRealmForBackup = Realm.getDefaultInstance();

String filePath = "";

try {
File dir = new File(Environment.DIRECTORY_DOWNLOADS);
File exportRealmFile = new File(Environment.DIRECTORY_DOWNLOADS, "backup.realm");
filePath = exportRealmFile.getPath();

if(!dir.exists()) {
dir.mkdirs();
}

if(!exportRealmFile.exists()) {
exportRealmFile.mkdirs();
Log.d("Path", "mkdirs :: " + filePath);
}

if(exportRealmFile.exists()) {
exportRealmFile.delete();
nowRealmForBackup.writeCopyTo(exportRealmFile);
Log.d("Backup", "Success to backup " + filePath);
} else {
Log.d("Backup", "Failed to Backup");
}

} catch(Exception e) {
e.printStackTrace();
} finally {
String msg = "File exported to Path: " + filePath;
Toast.makeText(getActivity().getApplicationContext(), msg, Toast.LENGTH_LONG).show();
Log.d(TAG, msg);

nowRealmForBackup.close();
}

}

当我运行上面的代码时,我的 logcat 说,

D/Path: mkdirs :: Download/backup.realm
D/Backup: Failed to Backup
D/ContentValues: File exported to Path: Download/backup.realm

我搜索了一段时间不起作用的原因,所以我检查了权限(因为我的手机运行在 SDK 23 Marshmellow 中)但是,我的权限被授予了。下面的代码检查 MainActivity.java 中的权限。

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this
, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE}
, REQUEST_RWAVAILABLE);
} else {
Log.e("Read and Write", "PERMISSION GRANTED");
}

我的 logcat 说“已授予权限”。

我认为问题是 exportRealmFile.mkdirs() 不起作用。你知道原因吗? (正如您在日志中看到的那样,Logcat 在该代码处没有收到任何异常”

========编辑:backupRealmNow() 已更改以检查我的包是否被允许。

public void backupRealmNow() {

Realm nowRealmForBackup = Realm.getDefaultInstance();

int REQUESTCODE_WRITE = 100;
int REQUESTCODE_READ = 200;

String filePath = "";

try {
File dir = new File(Environment.DIRECTORY_DOWNLOADS);
File exportRealmFile = new File(Environment.DIRECTORY_DOWNLOADS, "backup.realm");
filePath = exportRealmFile.getPath();

if(!dir.exists()) {
dir.mkdirs();
}

if(!exportRealmFile.exists()) {
exportRealmFile.mkdirs();
if(exportRealmFile.exists()) {
Log.d("mkdirs", "Success to make dir");
} else {
Log.d("mkdirs", "Failed to make dir");
if(PermissionChecker.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PermissionChecker.PERMISSION_GRANTED) {
Log.e("PermissionChecker", "WRITE_EXTERNAL_STORAGE PERMISSION_DENIED so request.");
requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUESTCODE_WRITE);
} else {
Log.e("PermissionChecker", "WRITE_EXTERNAL_STORAGE PERMISSION_GRANTED");

}
if(PermissionChecker.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE) != PermissionChecker.PERMISSION_GRANTED) {
Log.e("PermissionChecker", "READ_EXTERNAL_STORAGE PERMISSION_DENIED so request.");
requestPermissions(new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, REQUESTCODE_READ);
} else {
Log.e("PermissionChecker", "READ_EXTERNAL_STORAGE PERMISSION_GRANTED");
}
}
}

if(exportRealmFile.exists()) {
exportRealmFile.delete();
nowRealmForBackup.writeCopyTo(exportRealmFile);
Log.d("Backup", "Success to backup " + filePath);
} else {
Log.d("Backup", "Failed to Backup");
}

} catch(Exception e) {
e.printStackTrace();
} finally {
String msg = "File exported to Path: " + filePath;
Toast.makeText(getActivity().getApplicationContext(), msg, Toast.LENGTH_LONG).show();
Log.d(TAG, msg);

nowRealmForBackup.close();
}

}

我的 logcat 说,

D/mkdirs: Failed to make dir
E/PermissionChecker: WRITE_EXTERNAL_STORAGE PERMISSION_GRANTED
E/PermissionChecker: READ_EXTERNAL_STORAGE PERMISSION_GRANTED
D/Backup: Failed to Backup
D/ContentValues: File exported to Path: Download/backup.realm

最佳答案

您正在尝试在根目录(“/”)中创建目录“Download”。您需要使用Environment.getExternalStoragePublicDirectory来获取公共(public)存储区域的路径,您可以在其中实际创建文件和目录。

关于java - 由于 mkdirs(),我无法在外部目录中备份领域数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44062318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com