Updating a raw resource in an Android app

2020-04-17 05:47发布

my app is supposed to store some data in a text file. Every time when the app is started the data is read and every time the app is closed the data is wiped out from the text file and new data about the last session is saved.

Now I don't have problem reading in data:

InputStream is = getResources().openRawResource(R.raw.myFile);
BufferedReader br = new BufferedReader(new InputStreamReader(is));

The trouble is when I write to the file. Obviously it has to be accessed somehow by the resources but R.raw.myFile results only in InputStream and I have to pass a string to the openFileOutput. This is the code in question:

FileOutputStream fos = openFileOutput(getResources().openRawResource(R.raw.myFile), MODE_PRIVATE);

So it seems that this won't work. Is there any other way to write to the file in the raw folder?

1条回答
Ridiculous、
2楼-- · 2020-04-17 06:01

If you want to write to a file packaged in the raw folder, you will have to copy it to the local file system first. Resources contained in your raw directory in your project will be packaged inside your APK and will not be writeable at runtime. Consider looking at the Internal or External Data Storage APIs.

查看更多
登录 后发表回答