Reputation: 1800
I want to store my database inside the app folder as like we save images, so that when uninstall the app it wont clear, is that possible to do.
Can anyone suggest how to achieve otherwise give me alternate options
Thanks in advance
Upvotes: 0
Views: 46
Reputation: 7860
The apps internal folder will always be deleted when you uninstall an application. What you want is to create an external folder, on the SDcard this will not go when you delete your application.
Example:
public void writeToSDCard() throws IOException {
System.out.println("Write To Sd");
File f=new File("/data/data/com.yourapp/databases/Bdr");
FileInputStream fis=null;
FileOutputStream fos=null;
try{
fis=new FileInputStream(f);
fos=new FileOutputStream("/mnt/sdcard/dumped.db");
while(true){
int i=fis.read();
if(i!=-1){
fos.write(i);
}
else{
break;
}
}
fos.flush();
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
fos.close();
fis.close();
}
catch(IOException ioe){
// System.out.println(ioe);
}
}
}
The above code will write your Database to the external SDCard with the name being equal to dumped.db
and you will notice upon uninstall this will still persist on your SDcard
Dont forget to edit File f=new File("/data/data/com.yourapp/databases/Bdr");
according to your use case.
Also take special care that you dont hardcode the path to SDCard use Enviornmant.getExternalStorage()
instead
Upvotes: 1