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

时间:2017-05-19 05:49:35

标签: java android mkdirs

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

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

看条件

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。或者,您可以相互独立地检查和请求权限