当前位置:编程学习 > wap >>

Android导出APK里的数据库文件至SD卡【原创】

Android导出APK里的数据库文件至SD卡【原创】

 

  

    private class ExportDatabaseTask extends AsyncTask<Void, Void, String> {

        private final ProgressDialog dialog = new ProgressDialog(mContext);

 

 

        @Override

  protected void onPreExecute() {

            this.dialog.setMessage("正在导出数据库文件至SD卡,请稍候...");

            this.dialog.show();

        }

 



        @Override

  protected String doInBackground(final Void... args) {

            if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {

                return "未找到SD卡";

            }

      //setting.db为apk里的一个数据库文件

            File dbFile = new File(Environment.getDataDirectory() + "/data/com.goodboyenglish.leo/databases/setting.db");

            //"goodboyenglish_setting.db"为要备份至SD卡上的数据库文件名称

            File file = new File(Environment.getExternalStorageDirectory(), "goodboyenglish_setting.db");

 

            try {



                file.createNewFile();

                //拷贝文件

                copyFile(dbFile, file);

                return "成功导出数据库文件至SD卡!";

            } catch (IOException e) {

                return "导出失败!";

            }

        }

 



        @Override

  protected void onPostExecute(final String msg) {

            if (this.dialog.isShowing()) {

                this.dialog.dismiss();

            }

            Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();

        }

    }

 

 

 

  //拷贝文件的函数 src 为需要复制的文件,dst为目标文件(被src覆盖的文件)

//拷贝的过程其实就是把src文件里内容写入dst文件里的过程(从头写到尾)

    public static void copyFile(File src, File dst) throws IOException {

        FileChannel inChannel = new FileInputStream(src).getChannel();

        FileChannel outChannel = new FileOutputStream(dst).getChannel();

 

        try {

            inChannel.transferTo(0, inChannel.size(), outChannel);

        } finally {

 

        if (inChannel != null)

            inChannel.close();

        if (outChannel != null)

            outChannel.close();

        }

    }

     

    

    调用方式:new ExportDatabaseTask().execute(); --------------------编程问答-------------------- --------------------编程问答-------------------- 我郁闷~~没发群也删回帖~~
有木有搞错~~
我是来看帖的、、
别打击人家呀~~ --------------------编程问答-------------------- mark一下,谢谢分享!有用~~~
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,