zoukankan      html  css  js  c++  java
  • android程序复制数据库

    一般项目中我们把db文件放到assert或者raw目录下面,在程序第一次启动的时候复制到私有目录下面

    在使用过程中,老是发现复制不成功,私有目录下的db文件总是3072

    后来发现应该是使用ContentProvider的原因,它会先创建一个空的db。


    而我的程序在复制数据库的时候会判断私有目录下是否有数据库文件,如果有则不复制。

    现在改为用SharedPreferences一个字段判断是否第一次复制。

    第一次复制数据库的时候就算私有目录下有db文件,也删除。

    这样就ok了

    代码如下:

    Java代码  收藏代码
      1. public class CopyDataActivity extends Activity{  
      2.   
      3.     boolean needCopy = false;  
      4.     SharedPreferences mSP = null;  
      5.       
      6.     @Override  
      7.     protected void onCreate(Bundle savedInstanceState) {  
      8.         super.onCreate(savedInstanceState);  
      9.           
      10.         setContentView(R.layout.copy_data);  
      11.           
      12.         mSP = getSharedPreferences(Constants.PREFERENCES_NAME, MODE_PRIVATE);  
      13.           
      14.         needCopy = mSP.getBoolean("need_copy_data"true);  
      15.           
      16.         if(needCopy){  
      17.             handler.post(copyPlanThread);  
      18.         }else{  
      19.             goToMain();  
      20.         }  
      21.     }  
      22.       
      23.     private void goToMain(){  
      24.         mSP.edit().putBoolean("need_copy_data"false).commit();  
      25.         startActivity(new Intent(CopyDataActivity.this,LoginActivity.class));  
      26.         this.finish();  
      27.     }  
      28.       
      29.     private Handler handler = new Handler(){  
      30.           
      31.   
      32.         @Override  
      33.         public void handleMessage(Message msg) {  
      34.             super.handleMessage(msg);  
      35.               
      36.             int what = msg.what;  
      37.             int arg1 = msg.arg1;  
      38.               
      39.             if(what==1){  
      40.                                 //这里可以在页面显示复制进度什么的  
      41.                 Log.e("Copy","复制大小:"+arg1);  
      42.             }else{  
      43.                 goToMain();  
      44.                 mSP.edit().putBoolean("need_copy_data"false).commit();  
      45.             }  
      46.         }  
      47.     };  
      48.       
      49.     Runnable copyPlanThread = new Runnable() {  
      50.           
      51.         @Override  
      52.         public void run() {  
      53.             try{  
      54.                 copyDatabase();  
      55.             }catch(Exception e){  
      56.                 e.printStackTrace();  
      57.             }  
      58.         }  
      59.     };  
      60.       
      61.     private void copyDatabase() throws Exception{  
      62.         Log.e("Copy","copy start");  
      63.         File dbfile = new File(getFilesDir().getAbsolutePath() +File.separator+ "mydb.db";  
      64.         File dir = dbfile.getParentFile();  
      65.         if(dir.exists() == false){  
      66.             dir.mkdirs();  
      67.         }  
      68.         //把contentprovider生成的db删除  
      69.         if(dbfile.exists()){  
      70.             dbfile.delete();  
      71.         }  
      72.           
      73.         InputStream is = this.getResources().openRawResource(R.raw.library);   
      74.         FileOutputStream fos =  new FileOutputStream( dbfile);  
      75.           
      76.         byte[] buffer =new byte[1024];  
      77.         int size = 0;  
      78.         int length = 0//字节  
      79.         while( (length= is.read(buffer)) > 0){  
      80.             fos.write(buffer,0,length);  
      81.             size += length;  
      82.               
      83.             Message msg = new Message();  
      84.             msg.what = 1;  
      85.             msg.arg1 = size;  
      86.             handler.sendMessage(msg);  
      87.         }  
      88.         fos.flush();  
      89.         fos.close();  
      90.         is.close();  
      91.           
      92.         Log.e("Copy","copy end");  
      93.         Message msg = new Message();  
      94.         msg.what = 0;  
      95.         msg.arg1 = 0;  
      96.         handler.sendMessage(msg);  
      97.     }  

  • 相关阅读:
    10、Python的while与死循环
    8、 Python的if分支练习题
    7、 Python中的if多重判断
    6、Python的if判断和两重判断
    5、运算符
    4、数据类型:字典
    placeholder 颜色更改
    禁止video在苹果手机上的自动全屏播放
    点击label出发两次点击事件
    instanceof 和 typeof
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/3467410.html
Copyright © 2011-2022 走看看