Search code examples
javaandroidmkdirs

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


I am trying to export my realm database in external storage (actually in my cell phone)

When I press the button below method, named backupRealmNow(), works.

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();
    }

}

when I run above codes, my logcat said,

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

I search the reason why this doesn't work for a while, so I check the permission (because my cell phone is running in SDK 23 Marshmellow) But, my permission was granted. Below code is checking permission which is in 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");
    }

and my logcat said "PERMISSION GRANTED".

I think the problem is exportRealmFile.mkdirs() doesn't work. Do you know the reason why? (Logcat doesn't get any Exception at that codes as you see in log"

========= edit : backupRealmNow() was changed to check my package is permitted.

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();
    }

}

my logcat said,

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

Solution

  • You are trying to create the directory "Download" in the root directory ("/"). You need to use Environment.getExternalStoragePublicDirectory to get the path to a public storage area, where you can actually create files and directories.