My app needs to download a database from a link, heres the code I use to download the database adn save it on the sd card:
public void DownloadBD() {
try {
URL url = new URL(
"http://mylink/dbHandler.axd?SqliteDbVersion=0");
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
File SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(SDCardRoot, "DirLaguna.db");
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
int totalSize = urlConnection.getContentLength();
int downloadedSize = 0;
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
}
fileOutput.close();
location = file.getAbsolutePath();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Everything seems to work fine, but at the moment I want to query the database (I'm using raw querys) an error appears saying that the tables don't exist.
After that, I tried debugging the application, and after that I get the error saying that the database is corrupted. I think that the downloading process is what is making the database corrupt.
Any help will be apreciated, if I'm missing some code to share please tell me so!
Thanks in advance!