开发者社区 问答 正文

android 复杂外部数据库报错?报错

    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)

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


展开
收起
爱吃鱼的程序员 2020-06-14 17:35:22 759 分享 版权
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    /data/data/org.join.tts.sampleWords.db:openfailed:EACCES(Permissiondenied)

    已经有明显提示了

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

    应该加一个读写权限吧
    2020-06-14 17:35:38
    赞同 展开评论