Database won't remove when uninstall the Android Application

AndroidSqliteAndroid SqliteAndroid FileAndroid Database

Android Problem Overview


I have two major questions.

  1. Database won't delete when uninstall app.
  2. Downloaded files won't delete while unstable the app.

There is a database in my android application. I create it by java

class as follows.

public DataBaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public DataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
    super(context, name, factory, version, errorHandler);
}

@Override
public void onCreate(SQLiteDatabase db) {
    // creating required tables
    db.execSQL(CREATE_TABLE_QUOTES);
    db.execSQL(CREATE_TABLE_FILTERS);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // on upgrade drop older tables
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUOTES);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUOTES);
    // create new tables
    onCreate(db);
}

There is no specific path defined at the code for database.

This is the code how I download files. And there is specific path, But it is not allowed to create folder in Android>data>com.myapp as well.

public String downloadImage(String img_url, int i) {
        File sdCard = Environment.getExternalStorageDirectory();
        File dir = new File (sdCard.getAbsolutePath() + "/fog/images/filters");
        // Make sure the Pictures directory exists.
        dir.mkdirs();
        File destinationFile = new File(dir, "filter"+i);
        String filepath = null;
        try{
            URL url = new URL("http://fog.wrapper.io/uploads/category/"+img_url+".png");

            HttpURLConnection conection = (HttpURLConnection)url.openConnection();
            conection.setRequestMethod("GET");
            conection.setRequestProperty("Content-length", "0");
            conection.setUseCaches(false);
            conection.setAllowUserInteraction(false);
            conection.connect();

            int status = conection.getResponseCode();

            switch (status) {
                case 200:
                case 201:
                    FileOutputStream fileOutput = new FileOutputStream(destinationFile);
                    InputStream inputStream = conection.getInputStream();
                    int totalSize = conection.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;                            Log.i("Progress:","downloadedSize:"+downloadedSize+"totalSize:"+ totalSize) ;
                    }
                    fileOutput.close();
                    if(downloadedSize==totalSize) filepath = destinationFile.getPath();
                   Log.i("filepath:"," "+filepath) ;
                    return filepath;
            }

        } catch (IOException e) {
            Log.d("ImageManager", "Error: " + e);
        }
        return null;
    }
} // Get filters

Please help me. Sorry for bad English.

Android Solutions


Solution 1 - Android

in Android 6.0, google added new feature called Auto Backup.

when this option is on(default is on), Android system copies almost every directories and files that created by system, and upload it to user's google drive account.

When user reinstalls app, android automatically restore app's data, no matter how it was installed(via Play store, adb install, initial device setup).

> The restore operation occurs after the APK is installed, but before the app is available to be launched by the user.

android developers page : https://developer.android.com/guide/topics/data/autobackup.html

Solution 2 - Android

GUI WAY

If this is a personal account and you need to delete the backup for testing, you can go to drive.google.com for your account and navigate to the backups section.

Select a backup and you are given the option to delete the backups for a specific device:

Select backups and then a backup to delete

ADB SHELL WAY

You can also do this from the command line with the following:

adb shell bmgr wipe com.google.android.gms/.backup.BackupTransportService com.example.app

You can find more details related to this command here:

http://www.androiddocs.com/tools/help/bmgr.html#other

ADB SHELL BACKUP MANAGER USAGE

The usage for the command can be found here:

https://github.com/aosp-mirror/platform_frameworks_base/blob/6f357d3284a833cc50a990e14b39f389b8972254/cmds/bmgr/src/com/android/commands/bmgr/Bmgr.java#L443

    System.err.println("usage: bmgr [backup|restore|list|transport|run]");
    System.err.println("       bmgr backup PACKAGE");
    System.err.println("       bmgr enable BOOL");
    System.err.println("       bmgr enabled");
    System.err.println("       bmgr list transports");
    System.err.println("       bmgr list sets");
    System.err.println("       bmgr transport WHICH");
    System.err.println("       bmgr restore TOKEN");
    System.err.println("       bmgr restore TOKEN PACKAGE...");
    System.err.println("       bmgr restore PACKAGE");
    System.err.println("       bmgr run");
    System.err.println("       bmgr wipe TRANSPORT PACKAGE");
    System.err.println("");
    System.err.println("The 'backup' command schedules a backup pass for the named package.");
    System.err.println("Note that the backup pass will effectively be a no-op if the package");
    System.err.println("does not actually have changed data to store.");
    System.err.println("");
    System.err.println("The 'enable' command enables or disables the entire backup mechanism.");
    System.err.println("If the argument is 'true' it will be enabled, otherwise it will be");
    System.err.println("disabled.  When disabled, neither backup or restore operations will");
    System.err.println("be performed.");
    System.err.println("");
    System.err.println("The 'enabled' command reports the current enabled/disabled state of");
    System.err.println("the backup mechanism.");
    System.err.println("");
    System.err.println("The 'list transports' command reports the names of the backup transports");
    System.err.println("currently available on the device.  These names can be passed as arguments");
    System.err.println("to the 'transport' and 'wipe' commands.  The currently selected transport");
    System.err.println("is indicated with a '*' character.");
    System.err.println("");
    System.err.println("The 'list sets' command reports the token and name of each restore set");
    System.err.println("available to the device via the current transport.");
    System.err.println("");
    System.err.println("The 'transport' command designates the named transport as the currently");
    System.err.println("active one.  This setting is persistent across reboots.");
    System.err.println("");
    System.err.println("The 'restore' command when given just a restore token initiates a full-system");
    System.err.println("restore operation from the currently active transport.  It will deliver");
    System.err.println("the restore set designated by the TOKEN argument to each application");
    System.err.println("that had contributed data to that restore set.");
    System.err.println("");
    System.err.println("The 'restore' command when given a token and one or more package names");
    System.err.println("initiates a restore operation of just those given packages from the restore");
    System.err.println("set designated by the TOKEN argument.  It is effectively the same as the");
    System.err.println("'restore' operation supplying only a token, but applies a filter to the");
    System.err.println("set of applications to be restored.");
    System.err.println("");
    System.err.println("The 'restore' command when given just a package name intiates a restore of");
    System.err.println("just that one package according to the restore set selection algorithm");
    System.err.println("used by the RestoreSession.restorePackage() method.");
    System.err.println("");
    System.err.println("The 'run' command causes any scheduled backup operation to be initiated");
    System.err.println("immediately, without the usual waiting period for batching together");
    System.err.println("data changes.");
    System.err.println("");
    System.err.println("The 'wipe' command causes all backed-up data for the given package to be");
    System.err.println("erased from the given transport's storage.  The next backup operation");
    System.err.println("that the given application performs will rewrite its entire data set.");
    System.err.println("Transport names to use here are those reported by 'list transports'.");

Solution 3 - Android

The auto backup can be disabled by setting android:allowBackup="false" in AndroidManifest.xml

Solution 4 - Android

I cam across this question when I was looking for a solution to a similar problem related to GreenDao - slightly more in depth answer here but basically if your on api 23 you'll need to set allowBackup to false to be able to depend on the databases being cleared when you uninstall

https://stackoverflow.com/a/43046256/5298819

Solution 5 - Android

  1. Take a look at this SO answer:

https://stackoverflow.com/questions/9602787/what-happens-to-a-sqlite-database-when-app-is-removed

  1. Does your DB work (aside from not deleting when removing the app)?

  2. If it isn't working properly, you may want to take a look at:

http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html

Although this is not necessarily related to your issue, you might want to consider creating an open() and close() for your DB and use a SQLiteOpenHelper object in each - in open(), you would use sqliteopenhelperObj.getWriteableDatabase() and in close() you would use sqliteopenhelperObj.close().

http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#close

Edit:

  1. If you've downloaded files to your device during the process of testing an app, and you want to delete them, you can use the Device Monitor in Android Studio https://developer.android.com/tools/help/monitor.html There's a file manager that will allow you to see and edit files on your devices. You can also do this on the command line with the ADB (Android Debug Bridge) https://developer.android.com/tools/help/adb.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKZoNEView Question on Stackoverflow
Solution 1 - Androidyeonseok.seoView Answer on Stackoverflow
Solution 2 - Androiddazza5000View Answer on Stackoverflow
Solution 3 - AndroidFLashView Answer on Stackoverflow
Solution 4 - AndroidMichael.View Answer on Stackoverflow
Solution 5 - AndroidrobkriegerflowView Answer on Stackoverflow