I'm currently developing an Android app using OCR and I've reached the point where I'm calling the BaseAPI.init() method. I keep getting errors stating that the directory must contain tessdata as a subfolder. I've checked that the file directory contains the folder with the trainingdata file inside, and made sure I'm pointing to the right directory. I would really like to fix this.
The directory i'm pointing to is /mnt/sdcard/Image2Text/ . I've made sure that tessdata is a subfolder with the necessary language file inside.
Here is the code:
public static final String DATA_PATH = Environment.getExternalStorageDirectory().toString() +
"/Image2Text/";
....
File dir = new File(DATA_PATH + "tessdata");
dir.mkdirs();
if (!(new File(DATA_PATH + "tessdata/" + lang + ".traineddata")).exists()) {
try {
AssetManager assetManager = getAssets();
InputStream in = assetManager.open("eng.traineddata");
OutputStream out = new FileOutputStream(DATA_PATH
+ "tessdata/eng.traineddata");
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
} catch (IOException e) {}
}
TessBaseAPI baseAPI = new TessBaseAPI();
baseAPI.init(DATA_PATH, lang);
baseAPI.setImage(new File(path));
Like you say, the DATA_PATH directory must contain tessdata as a subfolder. So, if your tessdata folder was /data/data/tessdata, DATA_PATH would be /data/data
I hope that this helps!
EDIT: ak, I think I missunderstood!
Related
I'm trying put all subfolders of a folder in a zip file, so I'm doing this:
public static void zipFolder(String inputFolderPath, String outZipPath) {
try {
FileOutputStream fos = new FileOutputStream(outZipPath);
ZipOutputStream zos = new ZipOutputStream(fos);
File srcFile = new File(inputFolderPath);
File[] files = srcFile.listFiles();
Log.d("", "Zip directory: " + srcFile.getName());
for (int i = 0; i < files.length; i++) {
Log.d("", "Adding file: " + files[i].getName());
byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(files[i]);
zos.putNextEntry(new ZipEntry(files[i].getName()));
int length;
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
zos.closeEntry();
fis.close();
}
zos.close();
} catch (IOException ioe) {
Log.e("", ioe.getMessage());
}
I saw this code in this question
But, the code goes into loop because lenght is always 1024
The loop happens at the second file. I printed the fileList:
In this case I tried create the "aaaaaaa" file, the others are from another attempt and I didn't understand why they are showed, because the directory don't have none zip file.
PS: the directory that I'm trying compress has two subfolders. I don't know if this can influence.
I solved the problem :D
In my vision, the method don't consider subfolders, only files. In my case I have two folders inside files directory and the content inside them.
So, now I call the method for each subfolder and the outZipPath should be a public directory:
Controller.zipFolder(getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath(), Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/content_" +
Util.formatoData.format(data).replace("/", ".") + ".zip");
Sorry,
I have no experience with the Android file system, I am struggling to understand it via the documentation and the tutorials.
I am trying to copy a file from a location to the external storage of my app.
final File filetobecopied =item.getFile();
File path=getPrivateExternalStorageDir(mContext);
final File destination = new File(path,item.getName());
try
{copy(filetobecopied,destination);
}
catch (IOException e) {Log.e("",e.toString());}
public void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
Toast.makeText(mContext,"COPIED",Toast.LENGTH_SHORT).show();
}
public File getPrivateExternalStorageDir(Context context) {
File file = context.getExternalFilesDir(null);
if (!file.mkdirs()) {
Log.e("", "Directory not created");
}
return file;
}
I get the following error:
09-18 10:14:04.260: E/(7089): java.io.FileNotFoundException: /storage/emulated/0/Android/data/org.openintents.filemanager/files/2013-08-24 13.18.14.jpg: open failed: EISDIR (Is a directory)
http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String) use this example of code and use standart examples of google)
Try
final File destination = new File(path + "/" + item.getName());
instead.
I assume, folder directory is not created or your external storage state is not mounted.
Before you perform file operations, you should sanitize your path. Following code is a sample code that I often use.
public File sanitizePath(Context context) {
String state = android.os.Environment.getExternalStorageState();
File folder = null;
if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
folder = new File(context.getFilesDir() + path);
// path is the desired location that must be specified in your code.
}else{
folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + path);
}
if (!folder.exists()){
folder.mkdirs();
}
return folder;
}
And be sure about that, when your directory has to be created before you perform file operations.
Hope this will help.
EDIT: By the way, you have to add following permission to your manifest.xml file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I am developing an app that using tesseract android library functions. I succesfully compile the tesseract library and imported to my workspace. then I add the library reference to my app.But this method that I wrote causes the force close:
public static String BitmapOku (Bitmap image){
Bitmap image2=image.copy(Bitmap.Config.ARGB_8888, true);
TessBaseAPI baseApi = new TessBaseAPI();
baseApi.init("/mnt/sdcard/tessdata/eng.traineddata", "eng");
baseApi.setImage(image2);
String recognizedText = baseApi.getUTF8Text();
baseApi.end();
return recognizedText;
my question is---> what issues do I have to consider with a tesseract android ocr app and do I have to use leptonica tool in my app for image processing functios -for example rotating- (because I use Opencv 2.4.0)?
sometimes , even if the language file is inserted in the sdcard, there is a possibility that it wont be detected properly . So for efficiently establishing the file in the sdcard , following code may be useful.
String dirc= Environment.getExternalStorageDirectory().getPath().toString();
String[] paths = new String[] { dirc,dirc + "/tessdata/" };
for (String path :paths ) {
File dir = new File(path);
if (!dir.exists()) {
if (!dir.mkdirs()) {
System.out.println("Creation of directory on sdcard failed");
} else {
System.out.println("Creation of directory on sdcard successful");
}
}
}
// lang.traineddata file with the app (in assets folder)
// You can get them at:
// http://code.google.com/p/tesseract-ocr/downloads/list
// This area needs work and optimization
if (!(new File(dirc + "/tessdata/" + "eng.traineddata")).exists()) {
try {
AssetManager assetManager = getAssets();
InputStream in = assetManager.open("tessdata/eng.traineddata");
//GZIPInputStream gin = new GZIPInputStream(in);
OutputStream out = new FileOutputStream(dirc + "/tessdata/eng.traineddata");
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
//while ((lenf = gin.read(buff)) > 0) {
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
//gin.close();
out.close();
System.out.println("copied eng.traineddata");;
} catch (IOException e) {
e.printStackTrace();
}
}
here , the language file eng.traineddata is added to the assets folder in the project .
in assets folder, create a tessdata folder and put eng.traineddata in the folder ..
hope this solves your problem.
I am trying to create a folder and several subdirectory within it on the SD Card... I then want to transfer files that I have stored in /res/raw to that folder... I addition, I want this to only happen once, the first time the program is ever run. I realize that this is ridiculously open-ended, and that I am asking a lot... but any help would be greatly appreciated.
This will copy all files in the "clipart" subfolder of the .apk assets folder to the "clipart" subfolder of your app's folder on the SD card:
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
String basepath = extStorageDirectory + "/name of your app folder on the SD card";
//...
// in onCreate
File clipartdir = new File(basepath + "/clipart/");
if (!clipartdir.exists()) {
clipartdir.mkdirs();
copyClipart();
}
private void copyClipart() {
AssetManager assetManager = getResources().getAssets();
String[] files = null;
try {
files = assetManager.list("clipart");
} catch (Exception e) {
Log.e("read clipart ERROR", e.toString());
e.printStackTrace();
}
for(int i=0; i<files.length; i++) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open("clipart/" + files[i]);
out = new FileOutputStream(basepath + "/clipart/" + files[i]);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(Exception e) {
Log.e("copy clipart ERROR", e.toString());
e.printStackTrace();
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
I experienced a similar problem when using mkdirs(), however because running the command:
mkdir one/two
fails on Linux, then the method http://download.oracle.com/javase/1.4.2/docs/api/java/io/File.html#mkdirs() subsequently fails too. I guess this means there is no way to use mkdirs on Android? My (probably rather hacky) work-around was to create each necessary directory separately:
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
new File(extStorageDirectory + "/one/").mkdirs();
new File(extStorageDirectory + "/one/two/).mkdirs();
Is it possible to create files in subdirectories created under "/data/data/packagename/files/" directory using openFileOutput method in android?
ie there is a sub-directory by name "text" inside the "/data/data/packagename/files/" directory.
I want to use the openFileOutput (String name, int mode) method to write a file eg:sample.txt into this directory....
Is it possible to use openFileOutput method to do it....
Can any one help me on this....
Not with openFileOutput, but you can use the regular java.io.File methods.
java.io.File.mkdir()
to create a directory, and for example for copying (can adjust for creating) a file from sdcard to some data subdir:
public static final void copyfile(String srFile, String dtFile){
Log.d(MyApp.APP,"copyfile " + srFile + " -> " + dtFile );
try{
File f1 = new File(srFile);
File f2 = new File(dtFile);
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
Log.d(MyApp.APP,"File copied to " + f2.getAbsolutePath());
} catch(FileNotFoundException ex){
Log.e(MyApp.APP,"Error.",ex);
} catch(IOException e){
Log.e(MyApp.APP,"Error.",e);
}
}