Search code examples
androidtesseractandroid-resourcesillegalargumentexceptiontess-two

Android - Path to Raw folder in the Res folder not found


My Package is: com.abdulwasaetariq.odnvt. I have a folder named tessdata in res/raw/

In the Tesseract API, there is this init function whose documentation and prototype is:

/* @param datapath the parent directory of tessdata ending in a forward
 *            slash
 * @param language an ISO 639-3 string representing the language(s)
 * @return <code>true</code> on success
 */
public boolean init(String datapath, String language) {
    return init(datapath, language, OEM_DEFAULT);
}

Looking on the internet told me that the way to give path to a resource file is like: android.resource://com.packageName

But i am getting the error java.lang.IllegalArgumentException: Data path does not exist!

Here is my relevant code:

    private static void initTess() {
    String dataPath = "android.resource://"+ context.getPackageName() + "/raw/";
    Log.d(TAG, "initTess: dataPath: " + dataPath);
    tessBaseAPI.init(dataPath,"eng");
}

The context.getPackageName() does in fact get me the correct package name com.abdulwasaetariq.odnvt

I wonder where the fault is.


Solution

  • The problem is that android.resource:// is not a valid way to access resources in your app's res/raw/ folder, so the error message you see is being caused by the invalid path.

    Try moving your files to assets/ and using AssetManager to copy the files to your device instead.