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.     }  
  98. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值