Android Question getDirAssets

roberto64

Active Member
Licensed User
Longtime User
Hi, I would like to know where I am wrong in this java code, I state that already BAX the code works as below,
Error
Impossibile copiare ita traineddatajava.io.FileNotFoundException: /data/user/0/b4a.example/files/ita.traineddata (No such file or directory)

If File.IsDirectory(File.DirRootExternal,"tessdata") = False Then
' File.MakeDir(File.DirRootExternal, "tessdata")
' tessDataPath = File.DirRootExternal & "/tessdata"
' 'Copy Trainned Files to RootDir
' Dim fList As List = File.ListFiles(File.DirAssets)
' Dim fileName As String
' For i=0 To fList.Size-1
' fileName = fList.Get(i)
' If fileName.ToLowerCase.Contains("eng.traineddata") Then
' If File.Exists(tessDataPath,fileName)=False Then
' File.Copy(File.DirAssets,fileName, tessDataPath, fileName)
' End If
' End If
' Next
' End If

public void TessOCR() {
String[] paths = new String[]{TESSERACT_PATH + "tessdata/"};
for (String path : paths) {
File dir = new File(path);
if (!dir.exists()) {
if (!dir.mkdirs()) {
BA.Log("ERROR: Creation of directory" +path);
return;
} else {
BA.Log("Created directory"+ path);
}
}
}
if (!(new File(TESSERACT_PATH + "tessdata/" + DEFAULT_LANGUAGE + ".traineddata")).exists()) {
try {
String gifname = DEFAULT_LANGUAGE + ".traineddata";
BA.Log("Nome File "+ gifname);
// InputStream in = mContext.getAssets().open("tessdata/" + DEFAULT_LANGUAGE + ".traineddata");
InputStream in = new BufferedInputStream(new FileInputStream(new File(anywheresoftware.b4a.objects.streams.File.getDirAssets(),
gifname.toLowerCase(BA.cul))));
//InputStream in = new BufferedInputStream(new FileInputStream(new File(anywheresoftware.b4a.objects.streams.File.getDirAssets(), gifname.toLowerCase(BA.cul))));
OutputStream out = new FileOutputStream(new File(TESSERACT_PATH + "tessdata/", DEFAULT_LANGUAGE + ".traineddata"));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
in.close();
out.close();
BA.Log("Copied " + DEFAULT_LANGUAGE + " traineddata");
} catch (IOException e) {
BA.Log("Impossibile copiare " + DEFAULT_LANGUAGE + " traineddata"+e.toString());
}

}
/*
mTess = new TessBaseAPI();
mTess.setDebug(true);
mTess.init(DATA_PATH, lang);
*/

 

drgottjr

Well-Known Member
Licensed User
Longtime User
as i recall, /data/user/0/b4a.example/files/ita.traineddata is File.DirInternal
 
Upvote 0

roberto64

Active Member
Licensed User
Longtime User
The ita. traineddata must be copied from "DirAssets" to a directory either in the storage or in the smartphone directory. naturally in java code.
thank you
 
Upvote 0

drgottjr

Well-Known Member
Licensed User
Longtime User
somewhere you have changed file.dirassets to file.dirinternal. when you try to copy your file from file.dirinternal to some other location, the exception is thrown because there is no file in file.dirinternal. look carefully and slowly at your code (including parts that you did not post). i believe that somewhere you have changed a reference from file.dirassets to file.dirinternal. /data/user/0/b4a.example/files is file.dirinternal
 
Upvote 0
Top