I have two major questions.
- Database won't delete when uninstall app.
- 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.
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
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
- Take a look at this SO answer:
What happens to a Sqlite database when app is removed
Does your DB work (aside from not deleting when removing the app)?
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:
- 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
The auto backup can be disabled by setting android:allowBackup="false"
in AndroidManifest.xml
来源:https://stackoverflow.com/questions/35915039/database-wont-remove-when-uninstall-the-android-application