從apk的raw資料夾裡copy檔案到手機儲存(應用程式的data下)
阿新 • • 發佈:2019-02-15
private void initDBFiles() { File dir = new File("/tvdatabase/Database"); if (!dir.exists() || !dir.isDirectory()) { dir.mkdir(); } File fileFactory = new File(dir, "factory.db"); if (!fileFactory.exists()) { loadDbFile(R.raw.factory, fileFactory); } File fileUserSetting = new File(dir, "user_setting.db"); if (!fileUserSetting.exists()) { loadDbFile(R.raw.user_setting, fileUserSetting); } } private void loadDbFile(int rawId, File file) { InputStream dbInputStream = getResources().openRawResource(rawId); FileOutputStream fos = null; try { fos = new FileOutputStream(file); byte[] bytes = new byte[1024]; int length; while ((length = dbInputStream.read(bytes)) > 0) { fos.write(bytes, 0, length); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { fos.close(); dbInputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }