简体   繁体   中英

How to use the Android file system correctly?

I am starting a sample Android application and one of the things I need to have are two files stored with the application at install time. At runtime the app should be able to read from and write to those files.

It doesn't seem that much of a task but I've read through various instructions (incl developer.android.com, O'Reilly Learning Android and some similar questions here) and never got the final clue that lead to answer my question.

The files are small enough (100kb) to allow being installed with the .apk file which as far as I understood leads to being stored at data/data/packagename. In order to put the files there during install the simple solution I found was adding the files to the res/raw folder in my Eclipse project. I did so but wasn't able to find the files when I tested the app with the emulator and DDMS.

I am sure there is a major piece of tutorial or information I missed, if so please whack it right into my face. Otherwise I'd appreciate any help you can offer.

Put your pre-build files in /asset directory of your application.

And at first run of application copy those files to /data/data/<application_package>/files directory,

Now do your write and read operation on those files from internal storage location.

Note: Be sure yo are not allowed to do write operation on files which are in /asset or /res/raw directories ( as they are read only file system ).

Also if your files are at /res/raw directory,

you can get those file using inputstream like,

InputStream ins = getResources().openRawResource(R.raw.file);

Try this tutorial . It shows how to get InputStream from any file, that a stored in raw folder.

See Resources.openRawResource() issue Android for an example of reading a raw resource. openRawResource gives you an InputStream to read your data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM