开发者

android file io

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)?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜