1

I'm about to go nuts with this. I keep getting errors when trying to open a text file that's in my assets directory, whose full path name is

C:\Users\Dov\Google Drive\AndroidStudioProjects\WordyHelperton - Copy - Copy\
   app\src\main\assets

Even though we can SEE filename Dictionary.dic in the assets folder for my project...

enter image description here

... I keep getting errors that the file doesn't exist:

W/`````: Can't open <Dictionary.dic>
W/System.err: java.io.FileNotFoundException: Dictionary.dic
W/System.err:     at android.content.res.AssetManager.openAsset(Native Method)
W/System.err:     at android.content.res.AssetManager.open(AssetManager.java:316)
W/System.err:     at android.content.res.AssetManager.open(AssetManager.java:290)
W/System.err:     at com.dslomer64.servyhelperton.DatabaseConnector$LoadDatabase.doInBackground(DatabaseConnector.java:328)
W/System.err:     at com.dslomer64.servyhelperton.DatabaseConnector$LoadDatabase.doInBackground(DatabaseConnector.java:315)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err:     at java.lang.Thread.run(Thread.java:841)

Doc says you can use hierarchical name in the open statement:

W/`````: Can't open <C:\Users\Dov\Google Drive\AndroidStudioProjects\WordyHelperton - Copy - Copy\app\src\main\assets\Dictionary.dic>
W/System.err: java.io.FileNotFoundException: C:\Users\Dov\Google Drive\AndroidStudioProjects\WordyHelperton - Copy - Copy\app\src\main\assets\Dictionary.dic
W/System.err:     at android.content.res.AssetManager.openAsset(Native Method)
W/System.err:     at android.content.res.AssetManager.open(AssetManager.java:316)
W/System.err:     at android.content.res.AssetManager.open(AssetManager.java:290)
W/System.err:     at com.dslomer64.servyhelperton.DatabaseConnector$LoadDatabase.doInBackground(DatabaseConnector.java:330)
W/System.err:     at com.dslomer64.servyhelperton.DatabaseConnector$LoadDatabase.doInBackground(DatabaseConnector.java:317)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)

Same problem.

Can you see any problem with my code? The problem HAS to be obvious, but after two days of trying this and that and utterly failing, and it LOOKS so good and therefore MUST be obvious, but I CAN'T SEE IT....


I've included this in case it's not obvious from above. If this doesn't help, I'm on my own.

Here is how DatabaseConnector gets called in onCreate in MainActivity:

assets = getAssets();

dbc = new DatabaseConnector(getApplicationContext(), assets); // to create DB if needed

Here's how mAssets and SOURCE_NAME are defined; also have DatabaseConnector definition and its call to dbOpenHelper.

enter image description here

Here's how LoadDatabase is called from createDbIfNecessary:

  LoadDatabase
      __loadDb;
      __loadDb = new LoadDatabase();
      __loadDb.execute((Object[]) null);

EDIT

Another opinion:

enter image description here

EDIT 2

Please note that changing the filename in the code to lowercase doesn't help. AND it's a DOS file, NOT ANDROID. AND File is never leaving drive C:

public static String DATABASE_SOURCE = 
"C:\\Users\\Dov\\Desktop\\ServyHelperton\\app\\src\\main" +
  "\\assets\\dictionary.dic";

W/`````: Can't open <C:\Users\Dov\Desktop\ServyHelperton\app\src\main\assets\dictionary.dic>

W/System.err: java.io.FileNotFoundException: C:\Users\Dov\Desktop\ServyHelperton\app\src\main\assets\dictionary.dic

I could add more code to prove what I just said, but trust me. The DATABASE_SOURCE name is ALL I changed.

DSlomer64
  • 4,234
  • 4
  • 53
  • 88
  • Check this answer here. It might solve your problem. https://stackoverflow.com/questions/45908648/android-get-file-from-asset-raw/45908819#45908819 – Malik Ahsan Sep 15 '17 at 22:41
  • I have to vent to whoever downvoted and voted to Close. How can you vote this down WITHOUT REASON? It is ABSOLUTELY CLEAR and it WILL BE USEFUL TO ME if you would just STOOP DOWN TO MY LEVEL AND TELL ME WHAT'S WRONG. And voting to CLOSE on WHAT GROUNDS? HOW DO YOU KNOW HOW MUCH RESEARCH AND EFFORT WENT INTO GETTING IT THIS FAR? Do you see how much useful help I've received? NONE.Suggestions didn't help, but I appreciated both. So it's NOT THAT CLEAR what the problem is. I saw NO REASON to show all I tried, but THAT was SIGNIFICANT effort. And I looked at AND REFERRED TO DOCS. *How -1, close?* – DSlomer64 Sep 16 '17 at 06:00
  • Mark, you DID help me by directing my attention to more locally defining `stream` and `sDict`, the `InputStream` and `Scanner`. – DSlomer64 Sep 17 '17 at 15:41
  • About venting... see my last comment. If all the problem was was the 'D'..... arrgghh. – DSlomer64 Sep 17 '17 at 15:46

2 Answers2

1

It appears your path is for Dictionary.dic rather than dictionary.dic

See if that helps

Eldar M.
  • 387
  • 2
  • 11
  • This. Android is Linux, and most (if not all) Linux filesystems are case sensitive. – Kevin Krumwiede Sep 15 '17 at 22:46
  • I know quite well that Linux is case sensitive. But the file in question is a **DOS** file and is NOT going onto an Android device. AND NOTE that "file was not found" even when all lowercase. So NOW what? – DSlomer64 Sep 16 '17 at 06:03
  • Your Answer is NOT useful. That's why I have `mContext.getAssets`. Maybe I have the wrong `Context`. – DSlomer64 Sep 16 '17 at 06:28
  • in your 2nd edit, your path is ServyHelperton but in the beginning it was WordyHelperton -Copy -Copy , did you change it to ServyHelperton or was that the initial one? – Eldar M. Sep 16 '17 at 08:23
  • also, and this is bizarre if the case [check that your file isn't failing due to size](https://stackoverflow.com/questions/2860157/load-files-bigger-than-1m-from-assets-folder) – Eldar M. Sep 16 '17 at 08:47
  • 1
    I'm embarrassed. Even though `dictionary.dic` is a Windows file, I'm under the auspices of Android Studio, where cASe dOes MaTter. SO that was a problem. But the size of the file has never been an issue, So I owe an apology to Kevin and Eldar. – DSlomer64 Sep 17 '17 at 15:39
0

In the end, the fix was sort of easy or maybe dumb luck, because I'm not sure why making the InputStream and Scanner local to doInBackground cured the problem.

Refer to the first picture in the original Question. I made no significant changes to MainActivity, but here is the interesting line in it:

dbc = new DatabaseConnector(getApplicationContext(), getAssets());

This is what worked:

public class DatabaseConnector
{
  static Context mContext;

  public DatabaseConnector(Context _context, AssetManager _assets)
  {
    mAssets   = _assets;
    mContext  = _context;

    mDbOpenHelper = new DbOpenHelper(_context, DATABASE_NAME, null, 1);

    createDbIfNecessary();
  }

  private class DbOpenHelper extends SQLiteOpenHelper
  {
    DbOpenHelper(Context _context, String _name, CursorFactory _factory, int _version)
    {
      super(_context, _name, _factory, _version);
    }

    private class LoadDatabase extends AsyncTask<Object, Integer, Void>
    {
      protected Void doInBackground(Object[] params)
      {
        Scanner scDict = null; // ***** MOVING/ADDING THESE 
        InputStream stream;    // ***** TWO LINES HERE WAS KEY
        try{
          stream = mContext.getAssets().open(DATABASE_SOURCE);
          scDict = new Scanner(stream).useDelimiter("\r\n");
        }
        catch(IOException e){e.printStackTrace(); System.exit(69);}
      }
    }
  }
}
DSlomer64
  • 4,234
  • 4
  • 53
  • 88
  • I'm sorry that EVERY piece of advice, comments AND Answer, were completely wrong. And I'm also sorry that some dork downvoted and voted to close, because it IS A GOOD QUESTION WITH NO OBVIOUS SOLUTION. I'm also sorry that 3 people upvoted the other Answer. Not even close. Upvote, downvote, just don't THINK, is apparently how things are too often done here.. – DSlomer64 Sep 16 '17 at 08:51
  • I'm a jerk. It is NOT TRUE that the advice I received was useless. I'd hate to think that all along the only problem was `D` vs. `d` in dictionary. That would be impossibly and unbearably embarrassing. I'm tempted to put everything back to the way it was and then see if what I had would have worked with `D`ictionary.com. In THAT case it WAS a stupid question, deserving of both downvote and vote to close. I saw that `D` but said, hey, that's a Windows file. How could case matter? Well, it DOES TO ANDROID. Mea culpa, mea culpa, mea maxima culpa. – DSlomer64 Sep 17 '17 at 15:45