卸载 Android 应用程序时不会删除数据库

Database won't remove when uninstall the Android Application

我有两个主要问题。

  1. 卸载应用程序时不会删除数据库。
  2. 当应用不稳定时,下载的文件不会被删除。

我的 android 应用程序中有一个数据库。我通过 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);
}

代码中没有定义数据库的具体路径。

这是我下载文件的代码。并且有具体的路径,但是也不允许在Android>data>com.myapp中创建文件夹。

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

请帮助我。抱歉英语不好。

  1. 看看这个 SO 答案:

What happens to a Sqlite database when app is removed

  1. 您的数据库是否有效(除了删除应用程序时不删除)?

  2. 如果不能正常使用,您可能想看看:

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

虽然这不一定与您的问题相关,但您可能需要考虑为您的数据库创建一个 open()close(),并在每个对象中使用一个 SQLiteOpenHelper 对象 - 在 open(),您将使用 sqliteopenhelperObj.getWriteableDatabase(),而在 close() 中,您将使用 sqliteopenhelperObj.close()

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

编辑:

  1. 如果您在测试应用程序的过程中将文件下载到您的设备,并且您想要删除它们,您可以使用 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[=42 中的设备监视器=]

在 Android 6.0 中,google 添加了称为自动备份的新功能。

当此选项开启时(默认开启),Android系统复制几乎所有由系统创建的目录和文件,并将其上传到用户的google驱动器帐户。

当用户重新安装应用程序时,android 会自动恢复应用程序的数据,无论它是如何安装的(通过 Play 商店、adb 安装、初始设备设置)。

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

android 开发者页面: https://developer.android.com/guide/topics/data/autobackup.html

我在寻找与 GreenDao 相关的类似问题的解决方案时遇到了这个问题 - 此处的回答稍微深入一些,但基本上如果您在 api 23 上,则需要将 allowBackup 设置为false 能够依赖于卸载时清除的数据库

可以通过在 AndroidManifest.xml

中设置 android:allowBackup="false" 来禁用自动备份

GUI 方式

如果这是个人帐户并且您需要删除备份以进行测试,您可以转到 drive.google.com 为您的帐户导航到备份部分。

Select 一个备份,您可以选择删除特定设备的备份:

亚行SHELL方式

您也可以使用以下命令从命令行执行此操作:

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

您可以在此处找到与此命令相关的更多详细信息:

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

ADB SHELL 备份管理器使用

命令的用法可以在这里找到:

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'.");