2016-03-10 45 views
18

我有两个主要问题。卸载Android应用程序时数据库不会删除

  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 

请帮帮我。对不起,英文不好。

回答

2
  1. 看看这个苏答案:

What happens to a Sqlite database when app is removed

  • 是否(从删除应用程序时,不删除除外)你的数据库的工作?

  • 如果它不能正常工作,你可能想看一看:

  • 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

    编辑:

  • 如果你测试的应用程序的过程中,下载文件到您的设备,要删除它们,你可以使用Android Studio中的设备监视器https://developer.android.com/tools/help/monitor.html有一个文件管理器可让您在设备上查看和编辑文件。你也可以做到这一点与亚洲开发银行(安卓调试桥)在命令行上https://developer.android.com/tools/help/adb.html
  • +0

    非常感谢你,我会检查这一点,并尽快回复您,如果有任何问题。 – KZoNE

    +0

    我有同样的问题。我将我们的DatabaseHelper更新为新版本,并以调试模式安装了应用程序。在完成我新实现的功能后,我使用git返回了一些提交,然后尝试安装旧版本的应用程序。不幸的是,它不起作用,因为应用程序总是给出错误,它不能降级数据库。如果我在安装旧版本之前手动删除应用程序,甚至会出现此消息。所以我来到结果,数据库似乎并没有被删除,如果我卸载一个应用程序...真令人沮丧和烦人。 – jennymo

    43

    在安卓6.0,谷歌增加了一个叫做自动备份的新功能。

    当此选项开启时(默认为开启),Android系统几乎复制系统创建的每个目录和文件,并将其上传到用户的Google Drive帐户。

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

    还原操作发生在APK安装后,但应用程序可供用户启动之前。

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

    +11

    谢谢!这正是我的应用程序导致问题的原因。我通过将allowBackup设置为false来关闭应用程序中的“自动备份”来解决此问题。

    0

    我碰到这个问题,凸轮时,我一直在寻找一个解决方案,涉及GreenDao类似的问题 - 在深入回答稍微在这里,但基本上如果你对API 23你“会需要allowBackup设置为false,以便能够依赖于数据库被清除,当您卸载

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

    相关问题