溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

android 拷貝sqlite數(shù)據(jù)庫到本地sd卡的方法

發(fā)布時(shí)間:2020-09-13 07:22:33 來源:腳本之家 閱讀:229 作者:jingxian 欄目:移動(dòng)開發(fā)

sqlite小型數(shù)據(jù)庫,在開發(fā)的時(shí)候用于保存數(shù)據(jù),在這不做關(guān)于它的介紹,本文只是寫出了怎么拷貝應(yīng)用的數(shù)據(jù)到本地sd卡中。如:一個(gè)數(shù)據(jù)庫名為dandy.db的,拷貝到本地中叫seeker.db

代碼如下:

	/**
	 * 拷貝數(shù)據(jù)庫到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數(shù)據(jù)庫到本地sd卡的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI