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

I can't backup my realm database in External Directory because of mkdirs()

我正在尝试在外部存储中导出我的领域数据库(实际上在我的单元格中phone)

当我按下方法下方的按钮时,名为 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

我搜索了一段时间不起作用的原因,所以我检查了权限(因为我的单元格 phone 在 SDK 23 Marshmellow 中是 运行ning)但是,我的权限是的确。下面的代码正在检查 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 说 "PERMISSION GRANTED"。

我认为问题是 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

看情况

ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
        && 
ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED

这可以是 false 即使 WRITE_EXTERNAL_STORAGE 被拒绝并且不能保证会请求此权限。

尝试将AND运算符改为OR,真正理解WRITE_EXTERNAL_STORAGE是否被授予。或者您可以相互独立地检查和请求权限

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