Android 將數據庫文件保存至sdcard中,實現工具類:java
import android.content.Context; import android.os.Environment; import android.util.Log; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; /** * Created by SRain on 2015/4/28. * <p/> * 將數據庫拷貝到SDCard中指定位置 */ public class CopyDBToSDCard { //TODO 文件路徑選擇改爲本項目中文件讀取方式 public static boolean CopyDB(Context context) { boolean isSus = false; File dbFile = context.getDatabasePath(StaticCode.DB_NAME); InputStream myInput; try { myInput = new FileInputStream(dbFile); String filePath = ZipTool.getPath(context, "DB"); Log.e("filePath", filePath); if (filePath == null || filePath.equals("")) { Log.e("isSus", "未獲取到文件保存路徑"); return isSus; } OutputStream myOutput = new FileOutputStream(filePath + "/" + StaticCode.DB_NAME); byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer)) > 0) { myOutput.write(buffer, 0, length); } myOutput.flush(); myOutput.close(); myInput.close(); isSus = true; } catch (Exception e) { e.printStackTrace(); } return isSus; } }