Find SQLite database on Android

2

Next, I'm creating an Android application, where I use a local SQLite database, never gave any type of registration or database query, so I never needed to get the database directly in the emulator, but now I need the problem I can not find this bank, it's created, it has data, I just can not find it. I use Android Studio, I already looked at Android Device Monitor, but I can not open the folder "date", as it is empty. The strange thing is that if I try to create a "date / data" folder it says that the file already exists. How can I recover this bank?

    
asked by anonymous 01.03.2018 / 21:32

2 answers

4

The reason why sub-directories do not appear in the Data folder is adb not having permissions to read its content.

Use the Device File Explorer .

You can open it through View > Windows Tool > Device File Explorer or by clicking the button in the lower right side of Android Studio.

You can interact with the content by right-clicking to create a new one, saving the selected file or folder on the pc, deleting or synchronizing.

Notes:

  • Requires Android Studio 3.0 or higher
  • The content of the Data folder is only visible if the device is an emulator.
01.03.2018 / 22:19
4

If you can not find the file through the "Device File Explorer" window, you can use the following code:

try {

    /* Captura o arquivo original */
    File src = new File(Environment.getDataDirectory(), "//data//".concat(getPackageName()).concat("//databases//SEU-BANCO.db"));

    /* Captura o caminho do arquivo de destino */
    File dst = new File(Environment.getExternalStorageDirectory().getPath().concat("/SEU-BANCO-BACKUP.db"));

    /* Cria o arquivo de destino */
    dst.createNewFile();

    /* Abre o arquivo original e o de destino para leitura e escrita, respectivamente */
    FileInputStream inputStream = new FileInputStream(src);
    FileOutputStream outputStream = new FileOutputStream(dst);
    int b = 0;

    /* Ler o conteúdo em byte do arquivo original e escreve no arquivo de destino */
    while ((b = inputStream.read()) != -1) {
        outputStream.write(b);
    }

    /* Fecha as conexões */
    outputStream.close();
    inputStream.close();
} catch (Exception e) {
    e.printStackTrace();
}
    
01.03.2018 / 22:51