Expiredmind
Expiredmind

Reputation: 808

Environment.getExternalStorageDirectory returns bad value

public void exportDatabse(String databaseName) {

        try {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();
           if (sd.canWrite()) {
                String currentDBPath = "//data//"+getPackageName()+"//databases//"+databaseName+"";
                String backupDBPath ="//database.sql";
                File currentDB = new File(data, currentDBPath);
                File backupDB = new File(sd, backupDBPath);

                if (currentDB.exists()) {
                    FileChannel src = new FileInputStream(currentDB).getChannel();
                    FileChannel dst = new FileOutputStream(backupDB).getChannel();

                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                    Log.i(TAG,"coping database sucessfull");
                }
            }
        } catch (Exception e) {
        Log.i(TAG,"exception! "+ e.getMessage());
        }
    }

While debugging the sd file location File sd = Environment.getExternalStorageDirectory(); is

/storage/emulated/0

enter image description here

The copied file isn't visible in my SDcard. Moreover, I know the location of the sd file is incorrect. How can I get the database.sql file? UPDATE

enter image description here

Upvotes: 0

Views: 492

Answers (1)

Rolf ツ
Rolf ツ

Reputation: 8781

The value returned by Environment.getExternalStorageDirectory is correct and there is nothing wrong with it.

The problem here is that the Android Device Monitor doesn't directly support symlinks (symbolic links). You should manually follow/expand them in the File Explorer.

So in your case you need to expand the storage folder followed by the emulated and 0 folder.

If that doesn't work you might want to try to expand the storage folder followed by the self and primary folder.

TLDR: manually follow/expand the paths in the info column.

Edit: Try expanding the folders marked in yellow.

Android Device Monitor File Explorer

Upvotes: 2

Related Questions