我在资产文件夹中有一个sqlite文件。我使用的是已被引用上千次在其他堆栈溢出职位数据库管理器类:http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/Android - 无法打开资产文件
private void copyDatabase() throws IOException {
// Open your local db as the input stream
InputStream myInput = context.getAssets().open(DB_NAME);
的问题是open()方法(或getAssets?)是不断抛出IOException异常。我的路径和文件名:
private static final String DB_NAME = "attractioninfo";
private static final String DB_PATH = "/data/data/seattle.tourists/";
我的另一个相关问题是关于路径。我检查了测试手机上的文件(samsung galaxy sII),但我没有看到/ data/data/mypackagename/...任何地方。内部存储在哪里?我正在使用Android 2.2。
编辑:我做了更多的测试,我也发现,在第一次安装(这意味着没有数据库文件的电话内部存储,但它需要复制那里)这条线也不工作(我得到一个SQLiteException)
private boolean checkDatabase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
全码:
package seattle.tourists;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "attractioninfo";
private static final String DB_PATH = "/data/data/seattle.tourists/databases/";
private static final int DB_VERSION = 1;
private static final String TABLE_NAME = "ExtraInfo";
private SQLiteDatabase db;
private final Context context;
/**
* Constructor
* @param context application context
*/
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.context = context;
}
/**
* Creates empty database on system and rewrites it with existing database
* @throws IOException
*/
public void createDatabase() throws IOException {
boolean dbExist = checkDatabase();
if (dbExist) {
// do nothing, the database already exists;
}
else {
this.getReadableDatabase();
try {
copyDatabase();
}
catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* Check to see if a database exists
* @return true if database exists, false otherwise;
*/
private boolean checkDatabase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
catch (SQLiteException e) {
int x = 5;
}
if (checkDB != null)
checkDB.close();
return checkDB != null ? true : false;
}
/**
* Copes database from assets-folder to system folder, where it can be
* accessed and handled. This is done by transferring byte-stream.
* @throws IOException
*/
private void copyDatabase() throws IOException {
// Open your local db as the input stream
AssetManager assets = context.getAssets();
InputStream myInput = assets.open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte [] buffer = new byte[ 1024 ];
int length;
while ((length = myInput.read(buffer)) > 0)
myOutput.write(buffer, 0, length);
// close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public SQLiteDatabase openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH + DB_NAME;
return db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
public synchronized void close() {
if (db != null)
db.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
// do nothing
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// do nothing
}
}
这与我在代码中的内容完全相同,只是省略了AssetManager。尝试了AssetManager,它不起作用。 – ShrimpCrackers
发布您的完整代码.. –
Naeem,我感谢您的帮助。尝试了您的更改,但它不起作用。我得到同样的错误。 “无法加载数据库文件”和相同的例外情况。你的'manifest'文件中加入了 – ShrimpCrackers