0

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

当我按下名为 的方法下方的按钮时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");
    }

我的日志猫说“许可”。

我认为问题是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();
    }

}

我的日志猫说,

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
4

2 回答 2

1

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

于 2017-05-19T18:24:38.330 回答
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被授予。或者您可以相互独立地检查和请求权限

于 2017-05-19T06:14:32.220 回答