最近做一个OCR识别图片文字的功能,闲暇之余,提取项目中涉及的方法供大家参考.
这个方法是将assets下的指定文件或文件夹,放置到sd的指定目录下,代码中都有注释.
当然我们也可以探讨探讨OCR.
这个方法是将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();
}
}