如何为Android转换此代码?
POSModel model = new POSModelLoader()
.load(new File("en-pos-maxent.bin"));
我的onCreate上有这段代码
final AssetManager assetManager = getAssets();
final InputStream inputStream = null;
然后我尝试通过此代码访问我的资产
inputStream = assetManager.open("en-pos-maxent.bin");
然后我尝试将inputStream传递给代码new File(inputStream);
不确定如何将bin文件传递给模型。 我不知道如何将输入流用于java.io.File参数?
答案 0 :(得分:0)
尝试使用以下方法:
private File getFileFromAsset(){
AssetManager assetManager = getAssets();
InputStream inputStream = null;
OutputStream outputStream =null;
File file =null;
try {
inputStream = assetManager.open("en-pos-maxent.bin");
file = new File(getCacheDir(), "en-pos-maxent.bin");
outputStream = new FileOutputStream(file);
byte[] buffer = new byte[4 * 1024]; // or other buffer size
int read;
while ((read = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, read);
}
outputStream.flush();
} catch (Exception e) {
e.printStackTrace(); // handle exception, define IOException and others
} finally {
try {
if(outputStream!=null)
outputStream.close();
if(inputStream!=null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}}
return file;
}