copy database file to sdcard in android

后端 未结 2 674
梦谈多话
梦谈多话 2020-11-30 06:34

I am getting my database file by this code

  File dbFile=getDatabasePath(\"EdsysEyfsDB.db\");
               Log.v(\"database name checking\", dbFile.toStrin         


        
2条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 07:24

    Try this hope this helps you

    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 = "backupname.db";
                    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();
                    }
                }
            } catch (Exception e) {
    
            }
        }
    

    How to call

    exportDatabse("YourDBName");
    

    NOTE :

    Remember to add permission to write to external storage with , otherwise sd.canWrite() will be false.

提交回复
热议问题