0

I am trying to read some text from a .txt file in my project directory in android. I have a file called "test.txt" in my assets folder. However any text I write in this file causes a compiler error: Invalid resource directory name. Why is this? I thought I could put files into the assets directory without any problem. Am I missing something, do I need to declare all assets in the manifest or something? My assets folder is in the root project folder, eg Project/Assets/test.

Sreedev
  • 6,563
  • 5
  • 43
  • 66
Mike Baxter
  • 6,868
  • 17
  • 67
  • 115

3 Answers3

1

Your problem is located in the res folder where only folders with a set of allowed names by android can be stored.

My guess is, did you put the assets folder into the res folder? List your structure of folders in the res folder please.

methical
  • 132
  • 1
  • 7
1

However any text I write in this file causes a compiler error

You can not write anything in Assets at runtime. Assets folders don't exist on the phone. They're compiled into the package. Assets only provides reading facility.

If you want to write something, please use your external directory or Cache Directory provided by application.

Take a look at Storage Options.

Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
  • This is before compilation - I don't mean *programatically write*, I mean literally type into that file. Anything I type in to it causes eclipse to give me that error. – Mike Baxter Jul 25 '13 at 10:06
  • Before compilation means, before run your code. I have changed my words. :) – Chintan Rathod Jul 25 '13 at 10:10
  • FYI, http://stackoverflow.com/questions/3845227/how-can-i-edit-the-text-files-in-assets-folder-in-android, http://stackoverflow.com/questions/3760626/how-to-write-files-to-assets-folder-or-raw-folder-in-android – Chintan Rathod Jul 25 '13 at 10:13
  • I'm not trying to do it at **runtime** though, the purpose of this is to have a long SQL script that I run at startup that I would rather not hard-code. Could you suggest an alternative to using the assets folder? – Mike Baxter Jul 25 '13 at 10:23
  • is that script predefined? and can you store it as string as member of class? – Chintan Rathod Jul 25 '13 at 10:26
  • Yes it's pre-defined, it will only change with different versions of the app. I could store it just as a string, I was kind of hoping for a less messy option though. Is that the only way? – Mike Baxter Jul 25 '13 at 10:28
  • I think so.. because there is no facility given by android to modify application after build up means run time. If you put any file in any directory, you can not change it at all. So preferred idea to save that predefined script first in string format. When your application first time executes, write that script in `cache directory`. You can create it by using `Activity.this.getCacheDir()`. This will return a path to store cache files. For safer side, use `SharedPreferences` or external storage. – Chintan Rathod Jul 25 '13 at 10:32
  • @Teifi, Glad to help. Please accept and up vote to help other Googler :) – Chintan Rathod Jul 25 '13 at 10:41
0

I had the same error, when I added a new text file with some data to Assets folder by drag and drop. Cleaning the project helped.

Denis Kutlubaev
  • 15,320
  • 6
  • 84
  • 70