vendredi 2 janvier 2015

How to download sqlite database file and save in android device

I'm trying to download sqlite's .db file and replace it in my android device. Although i manage to download the file, it seems that the file size is not a match and when i try to open with sqliteman program it says this error: "Unable to open or create file test-sqlite5.db. It is probably not a database.". I suspect its due to charset issue because there area a lot of weird characters inside the .db file and probably when it is being downloaded, it is converted into some different characters? I've tried to use "UTF-8" and "UTF-16" but with same problem. Following is my code.



HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);



String line = "";

File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "test-sqlite5.db");
FileOutputStream fos = new FileOutputStream(f);

StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
// Read response until the end
try
{
while ((line = rd.readLine()) != null)
{
total.append(line);
}
} catch (IOException e)
{
Log.e(t, "error build string" + e.getMessage());
}


fos.write(total.toString().getBytes());
fos.close();

Aucun commentaire:

Enregistrer un commentaire