android 复杂外部数据库报错

lhbright 发布于 2015/03/05 16:42
阅读 373
收藏 1
    private SQLiteDatabase openDatabase(Context context,String dbfile) {
        try {
            if (!(new File(dbfile).exists())) {//判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库
                InputStream is = context.getAssets().open(DB_NAME); //欲导入的数据库
                FileOutputStream fos = new FileOutputStream(dbfile);
                byte[] buffer = new byte[BUFFER_SIZE];
                int count = 0;
                while ((count = is.read(buffer)) > 0) {
                    fos.write(buffer, 0, count);
                }
                fos.close();
                is.close();
            }
            SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,
                    null);
            return db;
        } catch (FileNotFoundException e) {
            Log.e("Database", "File not found");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e("Database", "IO exception");
            e.printStackTrace();
        }
        return null;

    }

把需要的数据库放在工程的assets目录下拷贝到/data/data/packageName/下报错:

03-05 16:40:58.291: W/System.err(22728): java.io.FileNotFoundException: /data/data/org.join.tts.sampleWords.db: open failed: EACCES (Permission denied)
03-05 16:40:58.301: W/System.err(22728):     at libcore.io.IoBridge.open(IoBridge.java:409)
03-05 16:40:58.301: W/System.err(22728):     at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
03-05 16:40:58.301: W/System.err(22728):     at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
03-05 16:40:58.301: W/System.err(22728):     at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
03-05 16:40:58.301: W/System.err(22728):     at com.ktt.db.DataBaseHelper.openDatabase(DataBaseHelper.java:149)
03-05 16:40:58.301: W/System.err(22728):     at org.join.tts.sample.TtsSampleActivity.onCreate(TtsSampleActivity.java:133)

是不是没有权限  应该加什么权限那 


加载中
0
alexwan
alexwan

/data/data/org.join.tts.sampleWords.db: open failed: EACCES (Permission denied)

已经有明显提示了

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

0
PentiumRabbit
PentiumRabbit
应该加一个读写权限吧
返回顶部
顶部