当前位置: 移动技术网 > 移动技术>移动开发>Android > android 拷贝sqlite数据库到本地sd卡的方法

android 拷贝sqlite数据库到本地sd卡的方法

2019年07月24日  | 移动技术网移动技术  | 我要评论

sqlite小型数据库,在开发的时候用于保存数据,在这不做关于它的介绍,本文只是写出了怎么拷贝应用的数据到本地sd卡中。如:一个数据库名为dandy.db的,拷贝到本地中叫seeker.db

代码如下:

	/**
	 * 拷贝数据库到sd卡
	 * 
	 * @deprecated <uses-permission android:name="android.permission.write_external_storage"/>
	 */
	public static void copydatabasetosd(){
		 if (!environment.media_mounted.equals(environment.getexternalstoragestate())) {
       return ;
     }
		 file dbfile = new file(mvpapplication.getapplication().getdatabasepath("dandy")+".db");
		 file file = new file(environment.getexternalstoragedirectory(), "seeker.db");
		 
		 filechannel inchannel = null,outchannel = null;
		 
		 try {
			file.createnewfile();
			inchannel = new fileinputstream(dbfile).getchannel();
			outchannel = new fileoutputstream(file).getchannel();
			inchannel.transferto(0, inchannel.size(), outchannel);
		} catch (exception e) {
			logutils.e(tag, "copy database to sd error.");
			e.printstacktrace();
		}finally{
			try {
				if (inchannel != null) {
					inchannel.close();
					inchannel = null;
				}
				if(outchannel != null){
					outchannel.close();
					outchannel = null;
				}
			} catch (ioexception e) {
				logutils.e(tag, "file close error.");
				e.printstacktrace();
			}
		}
	}

以上这篇android 拷贝sqlite数据库到本地sd卡的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网