2016-03-10 73 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

    相關問題