本文介绍了无法从资产文件夹中复制Android的SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在试图从资产文件夹复制SQLite数据库文件,所以我创建了一个SQLite数据库,并在我的程序中使用它,这里是code: -
I have been trying to copy a sqlite database file from assets folder so that i create a sqlite database and use it in my program, here is the code:-
首先 SqliteOpenHelperClass
public class DBHandler extends SQLiteOpenHelper {
protected static final String DATABASE_NAME_PRODUCTION = "productionComments.db";
public DBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory,
int version) {
super(context, name, factory, DATABASE_VERSION);
}
//copy database from assets folder (.sqlite) file to an empty database
public void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = context.getAssets().open("prod.db");
// Path to the just created empty db
String outFileName = "/data/data/com.qarun.qpcbeta/databases/"+DATABASE_NAME_PRODUCTION;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the input file to the output file
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();
}
现在在哪里我应该叫 copyDatabase()
法活动: -
Now the activity where i am supposed to call the copyDatabase()
method:-
public class ProductionCommentsActivity extends Activity {
private DBHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_production_comments);
dbHandler = new DBHandler(this, dbHandler.DATABASE_NAME_PRODUCTION, null, 1);
try {
dbHandler.copyDataBase();
//Toast.makeText(this,"Works",Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Log.d("copydb",e.getMessage());
//Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show();
}
}
下面是在logcat的错误: -
Here is the error in the logcat:-
Process: com.qarun.qpcbeta, PID: 1271
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.qarun.qpcbeta/com.qarun.qpcbeta.ProductionCommentsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.AssetManager android.content.Context.getAssets()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.AssetManager android.content.Context.getAssets()' on a null object reference
at com.qarun.qpcbeta.DBHandler.copyDataBase(DBHandler.java:100)
at com.qarun.qpcbeta.ProductionCommentsActivity.onCreate(ProductionCommentsActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
该应用程序停止工作(崩溃)当我打开 ProductionCommentsActivity
推荐答案
请您DbHelper这样,
Make your DbHelper like this,
public class DBHandler extends SQLiteOpenHelper {
protected static final String DATABASE_NAME_PRODUCTION = "productionComments.db";
private Context context;
public DBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory,
int version) {
super(context, name, factory, DATABASE_VERSION);
this.context = context;
}
//copy database from assets folder (.sqlite) file to an empty database
public void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = context.getAssets().open("prod.db");
// Path to the just created empty db
String outFileName = "/data/data/com.qarun.qpcbeta/databases/"+DATABASE_NAME_PRODUCTION;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the input file to the output file
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();
}
这篇关于无法从资产文件夹中复制Android的SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!