我有一個應用程序,我試圖設置一個相當大的SQLite數據庫(大約5000行的一個表)。我建立了數據庫類和一切,我的應用程序在小規模(400行)測試時正常工作,但現在當我想導入數據庫時,出現內存不足錯誤,我似乎無法找到一種方式來解決。Android內存不足錯誤:如何解決這個問題?
該數據庫最初是在我的Web服務器上的MySQL上,由於某種奇怪的原因,我無法將其轉換,但我設法生成一個文本文件,查詢添加了所有5000行,大小爲11.5mb。我在我的資產文件夾這個文件,我想這個把它變成我的DB:
public void onHandleIntent(Intent intent) {
DBAdapter db = new DBAdapter(getApplicationContext());
db.open();
try {
InputStream is = getAssets().open("verbs_sql.txt");
db.executeSQL(convertStreamToString(is));
} catch (IOException e) {}
db.close();
// Run main activity
Intent i = new Intent(DatabaseReceiver.this, BaseActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
DatabaseReceiver.this.startActivity(i);
}
public static String convertStreamToString(InputStream is) throws IOException {
Writer writer = new StringWriter();
char[] buffer = new char[2048];
try {
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
String text = writer.toString();
return text;
}
}
的內存不足的錯誤發生在StringWriter的(),所以它看起來喜歡它把那個大文件上到內存。我該如何解決這個問題?我也嘗試循環遍歷所有5000行,但在30秒之後,我又重新陷入了內存不足的境地。
因此,我應該在外部創建一個SQL文件,將其放在資產文件夾中,並找出如何在數據庫類中使用該SQL文件? – Qasim
查看[這裏](http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/),瞭解有關製作sqlite數據庫和讀取該數據庫的完整說明Android應用。 – antew
使用鏈接中提到的數據庫管理器,我能夠在文件中成功創建我的數據庫,並完成了指南的其餘部分:)謝謝 – Qasim