无法从android设备中的资产文件夹复制数据库

无法从android设备中的资产文件夹复制数据库,android,database,sqlite,assets,Android,Database,Sqlite,Assets,在我的代码中,一切似乎都正常,但我无法从android设备中的资产文件夹中获取数据库。保存在我的资产文件夹中的文件名为Database.sqlite 请检查我的代码,并向我建议解决方案或指出我哪里做错了 DatabaseHandler.java public class DatabaseHandler extends SQLiteOpenHelper { // All Static variables private static String TAG = DatabaseHandler.c

在我的代码中,一切似乎都正常,但我无法从android设备中的资产文件夹中获取数据库。保存在我的资产文件夹中的文件名为Database.sqlite 请检查我的代码,并向我建议解决方案或指出我哪里做错了

DatabaseHandler.java

public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables

private static String TAG = DatabaseHandler.class.getName();

// Database Path
private static String DB_PATH = "/data/data/com.WaqasAsgharBhalli.za_traders/databases/";
  private static String DB_NAME = "Database.sqlite";
  private SQLiteDatabase myDataBase = null; 
  private final Context myContext;

// Database Version
private static final int DATABASE_VERSION = 1;


public DatabaseHandler(Context context) {
    super(context, DB_NAME, null, DATABASE_VERSION);

    this.myContext = context;
    DB_PATH="/data/data/" + context.getPackageName() + "/" + "databases/";
    Log.v("log_tag", "DBPath: " + DB_PATH);
}

/**
 * Creates a empty database on the system and rewrites it with your own database.
 * */


public void createDatabase() throws IOException{
    boolean dbexist = checkDatabase();
    if(dbexist)
    {
        //System.out.println(" Database exists.");
    }
    else{
        this.getReadableDatabase();
    try{
            copyDatabase();
        }
        catch(IOException e){
            throw new Error("Error copying database");
        }
    }
}

/**
 * Check if the database already exist to avoid re-copying the file each time you open the application.
 * @return true if it exists, false if it doesn't
 */
public boolean checkDatabase() {
    //SQLiteDatabase checkdb = null;
    boolean checkdb = false;
    try{
        String myPath = DB_PATH + DB_NAME;
        File dbfile = new File(myPath);
        //checkdb = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);
        checkdb = dbfile.exists();
    }
    catch(SQLiteException e){
        System.out.println("Database doesn't exist");
    }

    return checkdb;
}

/**
 * Copies your database from your local assets-folder to the just created empty database in the
 * system folder, from where it can be accessed and handled.
 * This is done by transfering bytestream.
 * */
private void copyDatabase() throws IOException {

    //Open your local db as the input stream
    InputStream myinput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outfilename = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myoutput = new FileOutputStream("/data/data/com.WaqasAsgharBhalli.za_traders/databases/Database.sqlite");

    // transfer byte to inputfile to 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 void openDatabase() throws SQLException
{
    //Open the database
    String mypath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);

}

@Override
public synchronized void close(){
    if(myDataBase != null){
        myDataBase.close();
    }
    super.close();
}
}

maintactity.java类中调用它

    DatabaseHandler myDbHelper = new DatabaseHandler(this);

    boolean dbexist = myDbHelper.checkDatabase();
    if(dbexist)
    {
        System.out.println("Database exists");
        Toast.makeText(getApplicationContext(), "Database exists", Toast.LENGTH_LONG).show();
        myDbHelper.openDatabase(); 
    }
    else
    {
        System.out.println("Database doesn't exist");
        Toast.makeText(getApplicationContext(), "Database doesn't exists", Toast.LENGTH_LONG).show();
        try {
            myDbHelper.createDatabase();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

不要使用硬编码路径。@Karakuri在构造函数i中,使用context.getPackageName()获取路径。你能建议我该如何使用它吗?用
context.getDatabasePath()
而不是整行。Android的内部存储比人们想象的要复杂得多。不要对文件系统的结构做任何假设,使用上下文类提供给您的方法。请打包一个数据库,以便与您的应用程序一起分发。