將應用的數據庫複製到SD卡上面

在使用真機調試數據庫應用的時候,沒有Root過的手機,沒有辦法經過文件瀏覽器查看手機裏面的數據庫。沒有辦法,只能將數據庫複製到SD卡上面,而後再調試。代碼以下:java

package com.mm.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

import android.content.Context;
import android.util.Log;

public class Database2SD {
	
	public static void copyDatabase2SD(Context context,String databaseName){
		String fileName="/data/data/"+context.getPackageName()+"/databases/"+databaseName;
		
		File file=new File("/data/data/"+context.getPackageName());
		for(String path:file.list()){
			Log.e("Database2SD",path);
		}
		File databaseFile=new File(fileName);
		
		try {
			FileInputStream	fis = new FileInputStream(databaseFile);
			FileChannel inChannel=fis.getChannel();
			
			FileOutputStream fos=new FileOutputStream(new File("/sdcard/"+databaseName));
			FileChannel outChannel=fos.getChannel();
			outChannel.transferFrom(inChannel, 0, inChannel.size());
			
			fis.close();
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
}
相關文章
相關標籤/搜索