Android Sqlite file not deleted when uninstalling application?

When installing my application on an Android device, the database file is stored in the root folder of the SD card. But it should be placed on the path <SD Card_Root>\<Project_Name>\<dbfile> . After removing the application from the device, the database file, including the project folder, should also be deleted. So, please help me solve two problems as below:

1. After installing the apk file on the device, the database file should be placed in the path <SD Card_Root>\<Project_Name>\<dbfile> . 2. After deleting the apk file, the database file containing the root folder should be deleted automatically.

Please help me with your ideas / links.

Here is my code:

  import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import android.os.Environment; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteOpenHelper; public class DataBaseHelper extends SQLiteOpenHelper{ private static String DB_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() ; private static String DB_NAME = "Database.db"; private SQLiteDatabase myDataBase; private final Context myContext; public DataBaseHelper(Context context) { super(context, DB_NAME, null, 2); this.myContext = context; } public void createDataBase() throws IOException { // boolean dbExist = checkDataBase(); DB_PATH = Environment.getExternalStorageDirectory().getAbsolutePath(); if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File db = new File(DB_PATH, DB_NAME); if(!db.exists()) { db.createNewFile(); try { copyFromZipFile(); } catch (IOException e) { throw new Error("Error copying database",e); } } } } private void copyFromZipFile() throws IOException{ InputStream is = myContext.getResources().openRawResource(R.raw.database); // Path to the just created empty db File outFile = new File(DB_PATH ,DB_NAME); //Open the empty db as the output stream OutputStream myOutput = new FileOutputStream(outFile.getAbsolutePath()); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is)); try { ZipEntry ze = null; while ((ze = zis.getNextEntry()) != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[24 * 1024]; int count; while ((count = zis.read(buffer)) != -1) { baos.write(buffer, 0, count); //Log.d("", buffer.toString()); baos.writeTo(myOutput); baos.reset(); } baos.writeTo(myOutput); } } catch (IOException e) {} zis.close(); myOutput.flush(); myOutput.close(); is.close(); /*finally { zis.close(); myOutput.flush(); myOutput.close(); is.close(); } */ } private boolean checkDataBase(){ SQLiteDatabase checkDB = null; try{ String myPath = DB_PATH +""+ "/" + DB_NAME; checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); }catch(SQLiteException e){ //database does't exist yet. } if(checkDB != null){ checkDB.close(); } return checkDB != null ? true : false; } public void openDataBase() throws SQLException{ //Open the database String myPath = DB_PATH + "/" + DB_NAME; myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); } @Override public synchronized void close() { if(myDataBase != null) myDataBase.close(); super.close(); } @Override public void onCreate(SQLiteDatabase db) { String sql = "create table if not exists info(_id integer primary key autoincrement, Name text not null, firmName text not null);"; db.execSQL(sql); sql = "create table if not exists Client(_id integer primary key autoincrement, Name text not null, clientName text not null);"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS employee"); db.execSQL("DROP TABLE IF EXISTS info"); db.execSQL("DROP TABLE IF EXISTS Client"); onCreate(db); } public void onDestroy() { myDataBase.close(); } public Cursor getView(String sql) { if(myDataBase != null) { myDataBase.close(); } openDataBase(); Cursor xcur = myDataBase.rawQuery(sql,null); return xcur; } private void copyDataBase() throws IOException{ //Open your local db as the input stream InputStream myInput = myContext.getAssets().open(DB_NAME); // Path to the just created empty db String outFileName = DB_PATH + DB_NAME; //Open the empty db as the output stream OutputStream myOutput = new FileOutputStream(outFileName); //transfer bytes from the inputfile to the outputfile byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer))>0){ myOutput.write(buffer, 0, length); } //Close the streams myOutput.flush(); myOutput.close(); myInput.close(); } 

}

+4
source share
2 answers

If you want to delete the DB file (or any other file) when uninstalling the application, it should be placed below the path:

" sd_card/Android/data/your.package.name/ "

+13
source

You should always use Context.getExternalFilesDir(String) to find the path to store the files belonging to your application on the SD card. DO NOT USE any manual paths for this or any other standard places like this; always use the APIs that are provided by the platform in Context and Environment .

Please note that the platform has just begun to delete these files in the place where this API was introduced ( API level 8 ). Until now, you cannot store files on the SD card, which will lead to their removal when your application is deleted.

+16
source

Source: https://habr.com/ru/post/1383560/


All Articles