|
|
@@ -5,12 +5,15 @@ import android.content.Context; |
|
|
|
import android.os.Environment; |
|
|
|
import android.util.Log; |
|
|
|
|
|
|
|
import com.apkfuns.logutils.LogUtils; |
|
|
|
import com.bonait.bnframework.MainApplication; |
|
|
|
import com.bonait.bnframework.common.constant.ConfigName; |
|
|
|
import com.bonait.bnframework.common.db.file.DBHelper; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.FileInputStream; |
|
|
|
import java.io.FileOutputStream; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
|
|
|
|
/** |
|
|
@@ -46,13 +49,7 @@ public class SdCart { |
|
|
|
* 初始化数据库到SD卡 |
|
|
|
*/ |
|
|
|
public void initSD() { |
|
|
|
// File sdDir = null; |
|
|
|
// boolean sdCardExist = Environment.getExternalStorageState().equals( |
|
|
|
// Environment.MEDIA_MOUNTED); |
|
|
|
// if (sdCardExist) { |
|
|
|
// sdDir = Environment.getExternalStorageDirectory(); |
|
|
|
// ConfigName.getInstance().sdCardPath = sdDir.toString(); |
|
|
|
// } |
|
|
|
|
|
|
|
ConfigName.getInstance().sdCardPath = ConfigName.getFileRoot(MainApplication.getContext()); |
|
|
|
|
|
|
|
ConfigName.getInstance().appResRoot = ConfigName.getInstance().sdCardPath +ConfigName.dataPath; |
|
|
@@ -68,10 +65,28 @@ public class SdCart { |
|
|
|
if(!file.exists())//文件不存在那么创建数据库 |
|
|
|
{ |
|
|
|
Log.i("日志","初始化数据库:文件不存在准备新建!"); |
|
|
|
//1.复制本地文件到SD卡 |
|
|
|
copyFilesFassets(ConfigName.getInstance().dishesCon, ConfigName.getInstance().dbPath); |
|
|
|
//2.创建数据库结构 |
|
|
|
DBHelper.getInstance(ConfigName.getInstance().dishesCon).CreateTables(); |
|
|
|
|
|
|
|
//复制文件 |
|
|
|
if(!ConfigName.getInstance().isHighVersion){ |
|
|
|
|
|
|
|
File sdDir = null; |
|
|
|
String dbPath = ""; |
|
|
|
boolean sdCardExist = Environment.getExternalStorageState().equals( |
|
|
|
Environment.MEDIA_MOUNTED); |
|
|
|
if (sdCardExist) { |
|
|
|
sdDir = Environment.getExternalStorageDirectory(); |
|
|
|
dbPath = sdDir.toString()+ConfigName.dataPath; |
|
|
|
} |
|
|
|
File file2 = new File(dbPath); |
|
|
|
if(file2.exists()){ |
|
|
|
copyDirectory(dbPath,ConfigName.getInstance().appResRoot); |
|
|
|
} |
|
|
|
}else { |
|
|
|
//1.复制本地文件到SD卡 |
|
|
|
copyFilesFassets(ConfigName.getInstance().dishesCon, ConfigName.getInstance().dbPath); |
|
|
|
//2.创建数据库结构 |
|
|
|
DBHelper.getInstance(ConfigName.getInstance().dishesCon).CreateTables(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
ConfigName.getInstance(). AuthorizeCodePath=ConfigName.getInstance().appResRoot + "/AuthorizeCode.aes"; |
|
|
@@ -108,7 +123,109 @@ public class SdCart { |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
LogUtils.d("copyFilesFassets Exception"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 复制目录及其子目录中的所有文件到另一个目录 |
|
|
|
* |
|
|
|
* @param srcDir 源目录路径 |
|
|
|
* @param destDir 目标目录路径 |
|
|
|
* @return 复制是否成功 |
|
|
|
*/ |
|
|
|
public static boolean copyDirectory(String srcDir, String destDir) { |
|
|
|
LogUtils.d("copyDirectory srcDir="+srcDir+";destDir="+destDir); |
|
|
|
File srcFile = new File(srcDir); |
|
|
|
File destFile = new File(destDir); |
|
|
|
|
|
|
|
// 如果源目录不存在,则返回false |
|
|
|
if (!srcFile.exists()) { |
|
|
|
LogUtils.d("copyDirectory 如果源目录不存在"); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
// 如果目标目录不存在,则创建它 |
|
|
|
if (!destFile.exists()) { |
|
|
|
destFile.mkdirs(); |
|
|
|
} |
|
|
|
LogUtils.d("copyDirectory =="); |
|
|
|
// 列出源目录下的所有文件和文件夹 |
|
|
|
File[] files = srcFile.listFiles(); |
|
|
|
|
|
|
|
if (files != null) { |
|
|
|
for (File file : files) { |
|
|
|
// 构造源和目标文件路径 |
|
|
|
String srcFilePath = file.getAbsolutePath(); |
|
|
|
String destFilePath = destDir + File.separator + file.getName(); |
|
|
|
|
|
|
|
// 如果是文件,则直接复制 |
|
|
|
if (file.isFile()) { |
|
|
|
copyFile(srcFilePath, destFilePath); |
|
|
|
} |
|
|
|
// 如果是目录,则递归复制 |
|
|
|
else if (file.isDirectory()) { |
|
|
|
copyDirectory(srcFilePath, destFilePath); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
LogUtils.d("copyDirectory true"); |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 复制单个文件 |
|
|
|
* |
|
|
|
* @param sourceFile 源文件路径 |
|
|
|
* @param destFile 目标文件路径 |
|
|
|
* @return 复制是否成功 |
|
|
|
*/ |
|
|
|
private static boolean copyFile(String sourceFile, String destFile) { |
|
|
|
File sourceFileObj = new File(sourceFile); |
|
|
|
File destFileObj = new File(destFile); |
|
|
|
|
|
|
|
if (!sourceFileObj.isFile()) { |
|
|
|
// 如果源文件不存在,则复制失败 |
|
|
|
LogUtils.d("copyFile 如果源文件不存在"); |
|
|
|
|
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
// 确保目标文件所在的目录存在 |
|
|
|
File destFileParentDir = destFileObj.getParentFile(); |
|
|
|
if (!destFileParentDir.exists()) { |
|
|
|
destFileParentDir.mkdirs(); |
|
|
|
} |
|
|
|
|
|
|
|
FileInputStream fis = null; |
|
|
|
FileOutputStream fos = null; |
|
|
|
|
|
|
|
try { |
|
|
|
fis = new FileInputStream(sourceFileObj); |
|
|
|
fos = new FileOutputStream(destFileObj); |
|
|
|
|
|
|
|
byte[] buffer = new byte[1024]; |
|
|
|
int length; |
|
|
|
while ((length = fis.read(buffer)) > 0) { |
|
|
|
fos.write(buffer, 0, length); |
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return false; |
|
|
|
} finally { |
|
|
|
LogUtils.d("copyFile true"); |
|
|
|
// 关闭流 |
|
|
|
try { |
|
|
|
if (fis != null) fis.close(); |
|
|
|
if (fos != null) fos.close(); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |