Finding database path is impossible

后端 未结 2 725
有刺的猬
有刺的猬 2021-01-27 20:43

I am quite new to android development and right now I\'m trying to create a database through a class extending SQLiteOpenHelper. I am positively sure that the data is being sto

2条回答
  •  忘掉有多难
    2021-01-27 21:05

    if you are trying to get database which is stored in applicaiton package from device thn you will not get direct access from device /data/data/app packagename/databases/ ..you can access and modify using helperclass bt direct export nt poss AFAIK ... bt from emulator u cn gt

    enter image description here

    If you to export database from in sd card from application package

    try this

    private void copyFromDataPackgeToSdCard() throws IOException {
        try {
            File sdCard = Environment.getExternalStorageDirectory();
            File appDataDir = Environment.getDataDirectory();
            if (sdCard.canWrite()) {
                String currentDBPath = "//data//" + getPackageName()
                        + "//databases//"
                        + DatabaseImplementation.DATABASE_NAME;
                String backupDBPath = DatabaseImplementation.DATABASE_NAME;
                File currentDatabase = new File(appDataDir, currentDBPath);
                File backupDatabase = new File(sdCard, backupDBPath);
    
                if (currentDatabase.exists()) {
                    FileChannel src = new FileInputStream(currentDatabase)
                            .getChannel();
                    FileChannel dst = new FileOutputStream(backupDatabase)
                            .getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                }
            }
        } catch (Exception e) {
            Log.e("copyFromDataPackgeToSdCard", e.getMessage());
        }
    }
    

提交回复
热议问题