0

I download files from a URL to external storage, i then copy the data into internal storage, I then need to delete the file from the external storage, my copy method is as below:

public static void copyDataBase(String databaseName, String ExternalDatabaseName) throws IOException {

        String DB_NAME = databaseName;
        String NEW_DB_NAME = DB_NAME.replace("-", "_");
        String DB_PATH = MyApplication.getAppContext().getDatabasePath(NEW_DB_NAME).getPath();
        Log.d("pathx", "path: " + DB_PATH);

        // Open your local db as the input stream
        String fileName = ExternalDatabaseName;
        String newFileName = fileName.replace("_", "-");
        //String path = Environment.DIRECTORY_DOWNLOADS+"/"+fileName;
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);
        FileInputStream inputStream = new FileInputStream(file);

        // Path to the just created empty db
        String outFileName = DB_PATH;

        // Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        // transfer bytes from the input file to the output file
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }

        // Close the streams
        myOutput.flush();
        myOutput.close();
        inputStream.close();

        if(ContextCompat.checkSelfPermission(MyApplication.getAppContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            boolean deleted = file.delete();
        }
    }

As you can see after copying i attempt to delete the same file using the same path, however even though the deleted boolean returns true the file is not deleted from the devices, downloads file.

CHowsen
  • 25
  • 5

0 Answers0