這個方法是將assets下的指定文件或文件夾,放置到sd的指定目錄下,代碼中都有註釋.
當然我們也可以探討探討OCR.
/**
* 將assets下的文件放到sd指定目錄下
*
* @param context
* 上下文
* @param assetsPath
* assets下的路徑
* @param sdCardPath
* sd卡的路徑
*/
public static void putAssetsToSDCard(Context context, String assetsPath,
String sdCardPath) {
try {
String mString[] = context.getAssets().list(assetsPath);
if (mString.length == 0) { // 說明assetsPath爲空,或者assetsPath是一個文件
InputStream mIs = context.getAssets().open(assetsPath); // 讀取流
byte[] mByte = new byte[1024];
int bt = 0;
File file = new File(sdCardPath + File.separator
+ assetsPath.substring(assetsPath.lastIndexOf('/')));
if (!file.exists()){
file.createNewFile(); // 創建文件
}else{
return;//已經存在直接退出
}
FileOutputStream fos = new FileOutputStream(file); // 寫入流
while ((bt = mIs.read(mByte)) != -1) { // assets爲文件,從文件中讀取流
fos.write(mByte, 0, bt);// 寫入流到文件中
}
fos.flush();// 刷新緩衝區
mIs.close();// 關閉讀取流
fos.close();// 關閉寫入流
}
} else { // 當mString長度大於0,說明其爲文件夾
sdCardPath = sdCardPath + File.separator + assetsPath;
File file = new File(sdCardPath);
if (!file.exists())
file.mkdirs(); // 在sd下創建目錄
for (String stringFile : mString) { // 進行遞歸
putAssetsToSDCard(context, assetsPath + File.separator
+ stringFile, sdCardPath);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}