Java 从资产复制数据库时出错

Java 从资产复制数据库时出错,java,android,Java,Android,我在我的资产文件夹中有一个名为“word504.db”的文件,并试图将该文件复制到数据目录,以便能够读取它 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.la

我在我的资产文件夹中有一个名为“word504.db”的文件,并试图将该文件复制到数据目录,以便能够读取它

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

      String db_file = "word504.db" ;
      String db_path= this.getDatabasePath(db_file ).getPath();

        File dbFile = new File(db_path);
       if(!dbFile.exists())
        {
            try
            {
                copyFileFromAssets(db_file, db_path);
            }
            catch (IOException e) {
                Log.e("***********" , ""+ e.getMessage());
            }
        }


    }

    private void copyFileFromAssets(String name,String dest) throws IOException
    {
        InputStream mInput = this.getAssets().open(name);
        OutputStream mOutput = new FileOutputStream(dest);

        byte[] mBuffer = new byte[1024];
        int mLength;
        while ((mLength = mInput.read(mBuffer))>0)
        {
            mOutput.write(mBuffer, 0, mLength);
        }
        mOutput.flush();
        mOutput.close();
        mInput.close();
    }
}
但我的日志中有个错误:

/data/data/org.zanjan.words504/databases/word504.db:打开失败: eNONT(无此类文件或目录)

提前感谢

试试这个

DataBaseHalper.class



 public static final String DATABASE_NAME = "recipe.db";
    public final static String DATABASE_PATH = "/data/data/"+ MobyiUtils.PACKAGE_NAME + "/databases/";
    public static final int DATABASE_VERSION = 1;

 public void createDatabase()  {
        boolean dbExist1 = checkDataBase();
        if (!dbExist1) {
            this.getReadableDatabase();
            try {
                this.close();
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
        }
    }

    // Check database already exist or not
    private boolean checkDataBase() {
        boolean checkDB = false;
        try {
            String myPath = DATABASE_PATH + DATABASE_NAME;
            File dbfile = new File(myPath);
            checkDB = dbfile.exists();
        } catch (SQLiteException e) {
        }
        return checkDB;
    }

    // Copies your database from your local assets-folder to the just created
    // empty database in the system folder
    public void copyDataBase() throws IOException {
        String outFileName = DATABASE_PATH + DATABASE_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);
        InputStream myInput = mContext.getAssets().open(DATABASE_NAME);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        myInput.close();
        myOutput.flush();
        myOutput.close();
    }
主要活动类

 helper = new DataBaseHelper(MainActivity.this);
        helper.createDatabase();
        helper.openDatabase();
        helper.close();

谢谢你和一票支持你的帮助。但我只是想知道我的代码有什么问题,我如何解决这个问题,你检查过数据库目录是否存在吗?我为什么要检查?再详细一点回答怎么样,这样我就可以接受这个回答了…谢谢???首先告诉我这是否是问题所在。