how to make a copy of sqlite file to external memory

1

I'm trying to create a copy of the sd memory, but the procedure I do does not do anything or I get an error, I have the write permissions, this is the code I'm implementing

 public static boolean copyFile(String from, String to) {
        boolean result = false;
        try{
            File dir = new File(to.substring(0, to.lastIndexOf('/')));
            dir.mkdirs();
            File tof = new File(dir, to.substring(to.lastIndexOf('/') + 1));
            int byteread;
            File oldfile = new File(from);

            if(oldfile.exists()){
               // System.exit(0);
                InputStream inStream = new FileInputStream(from);
                FileOutputStream fs = new FileOutputStream(tof);
                byte[] buffer = new byte[1024];
                while((byteread = inStream.read(buffer)) != -1){
                    fs.write(buffer, 0, byteread);
                }
                inStream.close();
                fs.close();

            }
            result = true;
        }catch (Exception e){
            Log.e("copyFile", "Error copiando archivo: " + e.getMessage());

        }
        return result;
    }

from here I call the procedure

 String nombd = "DBUsuarios";

               //Obtiene ruta de base de datos origen.
            String pathDB = getDatabasePath(nombd).toString();
              //Copia base de datos a destino definido.
            copyFile(pathDB,""+Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));

Permissions

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
asked by juan perez 04.10.2018 в 23:02
source

2 answers

0

The validation that you are doing this may be why the code is not executed and it does not give an error. Notice what you're doing:

if(oldfile.exists()){...}

when it is assumed that oldfile has to exist and then copy it, it would be enough to do:

if(!oldfile.exists()){...}

Hopefully, that alone, luck

    
answered by 04.10.2018 в 23:39
0

Hi Juan, I see that the code was obtained from this example:

How to copy a Sqlite database or file from internal storage to external storage?

I modified my answer on that question because to use the permission WRITE_EXTERNAL_STORAGE on devices with operating systems greater than Android 6.0 This must be manually requested:

//Verifica permisos para Android 6.0+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){

     checkExternalStoragePermission();
}

Use this method to request permission:

private void checkExternalStoragePermission() {
    int permissionCheck = ContextCompat.checkSelfPermission(
            this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        Log.i("Mensaje", "No se tiene permiso para leer.");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 225);
    } else {
        Log.i("Mensaje", "Se tiene permiso para leer!");
    }
}
    
answered by 04.10.2018 в 23:55