从资产中复制数据库文件

时间:2014-09-17 18:17:59

标签: android sqlite

我正在使用此代码将资产数据库文件从资产转移到/ data / data / app / databases但是我得到了这个异常并且应用程序崩溃了。有什么问题?

一旦工作! 我每次尝试代码时都会卸载应用程序!

 public static void copyFromAsset(Context context,String filename) throws IOException {

       try{
            // CHECK IS EXISTS OR NOT
            SQLiteDatabase dbe = SQLiteDatabase.openDatabase("/data/data/"+context.getPackageName()+"/databases/"+filename,null, 0);
            dbe.close();
        }
        catch(Exception e)
        {
            // COPY IF NOT EXISTS
            AssetManager am = context.getAssets();
            OutputStream os = new FileOutputStream("/data/data/"+context.getPackageName()+"/databases/"+filename);
            byte[] b = new byte[1024];
            int r;
            InputStream is = am.open(filename);
            while ((r = is.read(b)) != -1) {
                 os.write(b, 0, r);
                 Log.i("Writing", "Is processing...");
           }
           is.close();
           os.close();
        }

   }

09-17 22:34:17.599: W/System.err(15674):    at libcore.io.Posix.open(Native Method)
09-17 22:34:17.609: W/System.err(15674):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
09-17 22:34:17.609: W/System.err(15674):    at libcore.io.IoBridge.open(IoBridge.java:403)
09-17 22:34:17.609: W/System.err(15674):    ... 12 more
09-17 22:34:17.609: W/System.err(15674): java.io.FileNotFoundException: /data/data/ir.aiga.apps.quran/databases/database_surainfo: open failed: ENOENT (No such file or directory)
09-17 22:34:17.619: W/System.err(15674):    at libcore.io.IoBridge.open(IoBridge.java:419)
09-17 22:34:17.619: W/System.err(15674):    at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
09-17 22:34:17.619: W/System.err(15674):    at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
09-17 22:34:17.619: W/System.err(15674):    at ir.aiga.apps.quran.classes.FileOperations.copyFromAsset(FileOperations.java:106)
09-17 22:34:17.619: W/System.err(15674):    at ir.aiga.apps.quran.MainActivity$ProgressHorizantol.doInBackground(MainActivity.java:736)
09-17 22:34:17.619: W/System.err(15674):    at ir.aiga.apps.quran.MainActivity$ProgressHorizantol.doInBackground(MainActivity.java:1)
09-17 22:34:17.619: W/System.err(15674):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-17 22:34:17.619: W/System.err(15674):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-17 22:34:17.619: W/System.err(15674):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-17 22:34:17.619: W/System.err(15674):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-17 22:34:17.619: W/System.err(15674):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-17 22:34:17.619: W/System.err(15674):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-17 22:34:17.619: W/System.err(15674):    at java.lang.Thread.run(Thread.java:856)
09-17 22:34:17.619: W/System.err(15674): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
09-17 22:34:17.619: W/System.err(15674):    at libcore.io.Posix.open(Native Method)
09-17 22:34:17.619: W/System.err(15674):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
09-17 22:34:17.619: W/System.err(15674):    at libcore.io.IoBridge.open(IoBridge.java:403)
09-17 22:34:17.619: W/System.err(15674):    ... 12 more

0 个答案:

没有答案