1

i'm trying to create an app that uses a text file(holding values that will resolve to enums) to contruct a visual interface for the app to display. the question is a little complex so I think it would be better to just explein the outcome:

what I want to do is read the file 2 characters(shall be numbers) at a time, that will then be concatonated to a string [str] (with a control character) so that I can tack that string into R.drawable [R.drawable.str] to get back and display the correct image.

more specifically is making a string to give to R.drawable even possible to resolve the way I want to, or do I have to dance around it?

at some point in my apps development lifecycle I will be implementing the ability to also generate these files as well to be used in divice. so is it possible to insure the user has no access to them but they can still exist as a form of text file?

is there anything specific that I need in order to read from a file on the system in "known directory"?

can I use carrage-return-line-feed when wtritting my text files and do I need to do anything beyond control code \n to manage it, as forced returns will make it more readable and can add another level of error checking to prevent modification, and system read errors(should they occur)?

gardian06
  • 1,496
  • 3
  • 20
  • 34

1 Answers1

1

For the scenario where you want to provide some text files with the app, you can include them in the project's /res/assets directory and then perhaps copy them on 'first run' to the app's /files directory in the phone's internal memory.

The /files directory would also be a good place to store any files the user creates if you don't want them to have direct access to the files themselves (internal memory can only be accessed if the user has root access to the phone).

As for concatenating a text string for accessing resources, this wont work as all R.foo.bar identifiers are actually textual 'keys' to integer ids. In other words R.drawable.my_icon, for example, only exists as a 'string' in the compiler environment and is translated to an integer at build time.

You might consider providing the drawables with names such as image_01, image_02 and putting them in your assets directory - you could then refer to them / access them by filename rather than resource identifier.

EDIT: For an example of copying files from the assets directory to the phone sdcard, see the answer to this question...Android: How to copy files in 'assets' to sdcard?

To copy to the internal /files directory, you would replace this line...

out = new FileOutputStream("/sdcard/" + files[i]);

...with something similar to...

out = Context.openFileOutput(files[i], MODE_PRIVATE);

openFileOutput(...) automatically creates a file which can be written to in the app's /files directory. Similarly, at a later date, you can use openFileInput(...) to read from the /files directory.

Community
  • 1
  • 1
Squonk
  • 48,735
  • 19
  • 103
  • 135
  • is there any way that I could see some kind of tutorial, blog-post, dev-note on this if there is such as it makes sence but I don't know how to actually do it. – gardian06 Apr 10 '11 at 22:02
  • I just thought of this and was wanting some imput. I was under the understanding that anything in the assets directory has a higher overhead as it is possible to exist outside of the singleton framework that drawable uses, so wouldn't I be able to maintain the images(about 60 or so) in the drawables, and then use a case switch inside a for loop that reads the file to construct the view. – gardian06 Apr 11 '11 at 00:22
  • @gardian06: Using a switch will work fine for resource ids - you specify the cases at compile time using their R.foo.bar form and they'll be resolved at runtime. It does depend on what you're trying to achieve though which may mean it works better in some situations than others. – Squonk Apr 11 '11 at 04:28