获得公共路径下的文件夹目录:
public class GetDir {
// 获取Excel文件夹
public static String getDir(String pathname) {
// SD卡指定文件夹
String sdcardPath = Environment.getExternalStorageDirectory().toString();
File dir = new File(sdcardPath + File.separator + pathname /*+ File.separator + "Files"*/);
if (dir.exists()) {
return dir.toString();
} else {
dir.mkdirs();
Log.e("BAG", "保存路径不存在,");
return dir.toString();
}
}
}
构建数据库:
void init_db(){
String pathname = GetDir.getDir(Constant_11.folder); // 这个路径,卸载app也无法清除文件,必须手动清除,才能重新构建数据库 /storage/emulated/0/android.system.data Constant_11.folder:android.system.data
File file = new File(pathname,Constant_11.file); //Constant_11.file: data_global.db
try{
if(!file.exists()){
file.createNewFile();
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
this.sendBroadcast(intent);
}
} catch (IOException e) {
Log.v("main", "failed1");
e.printStackTrace();
}
try {
Constant_11.dbHelper = new SQLiteHelper(this, pathname+"/"+Constant_11.file, null, Constant_11.DB_VERSION);
Constant_11.db = Constant_11.dbHelper.getWritableDatabase(); // 调用SQLiteHelper.OnCreate()
Log.v("main", "new db");
} catch (IllegalArgumentException e) {
Log.v("main", "failed2");
e.printStackTrace();
++Constant_11.DB_VERSION;
Constant_11.dbHelper.onUpgrade(Constant_11.db, --Constant_11.DB_VERSION, Constant_11.DB_VERSION);
}
}
其中:
Constant_11.folder
:android.system.data
;
Constant_11.file
: data_global.db
。
以上是将数据库文件存放在:/storage/emulated/0/android.system.data/
下。
这个路径,卸载app也无法清除文件,必须手动清除,才能重新构建数据库 。