我有兩個主要問題。卸載Android應用程序時數據庫不會刪除
- 數據庫在卸載應用程序時不會被刪除。
- 下載的文件不會在應用程序不穩定時被刪除。
在我的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
請幫幫我。對不起,英文不好。
非常感謝你,我會檢查這一點,並儘快回覆您,如果有任何問題。 – KZoNE
我有同樣的問題。我將我們的DatabaseHelper更新爲新版本,並以調試模式安裝了應用程序。在完成我新實現的功能後,我使用git返回了一些提交,然後嘗試安裝舊版本的應用程序。不幸的是,它不起作用,因爲應用程序總是給出錯誤,它不能降級數據庫。如果我在安裝舊版本之前手動刪除應用程序,甚至會出現此消息。所以我來到結果,數據庫似乎並沒有被刪除,如果我卸載一個應用程序...真令人沮喪和煩人。 – jennymo