12

My program opens a .txt file from the assets folder and reads from it. Here is the code:

           AssetManager myAssetManager = myContext.getAssets();
           try{
                   InputStream is = myAssetManager.open("databaseeleven.txt");
                   byte[] bytes = new byte[is.available()];
                   is.read(bytes);
                   commands = new String(bytes);

           } catch(IOException e){
                   Toast.makeText(myContext, e.getMessage(), Toast.LENGTH_SHORT).show();
                   e.printStackTrace();
           }//try-catch

I have noticed that when I make changes to the file called databaseeleven.txt and save the file, my changes aren't reflected on the emulator when I run my program again. The project is saved to a thumb drive. I checked it to make sure there's only one file with that name, and it is up to date. I know the application is re-downloaded because of changes to the code. I'm using egit, Eclipse version 3.6.2, and ADT version 10.0.1. Does anybody know why my program isn't working off this saved file?

Update: Refreshing and then cleaning the project again doesn't help.

sthompso
  • 131
  • 1
  • 4
  • 1
    This question is possible duplicate of [how-can-i-edit-the-text-files-in-assets-folder-in-android](http://stackoverflow.com/questions/3845227/how-can-i-edit-the-text-files-in-assets-folder-in-android) – Samuel May 04 '11 at 00:46
  • 5
    I don't think it's a duplicate, since the original poster is changing the asset file before rebuilding, not at runtime. – Ellen Spertus May 04 '11 at 22:14
  • What happens if you try installing the app to a different phone? Does it have the latest version of the assets file or an earlier one? Ditto for re-installing it on the first phone after explicitly deleting it? – Ellen Spertus May 04 '11 at 22:16
  • In both cases, the changes in the .txt file do show up. – sthompso May 09 '11 at 19:08
  • Bummer: the bounty is about to expire with no solution. :-( – Ellen Spertus May 10 '11 at 21:47
  • i'm sorry about the bounty expiration, before you guys are satisfied with a reason. :-( – Samuel May 11 '11 at 01:00
  • Strange that clean doesn't work. In my case, I make a change to the asset file, then change a .scala file (usually just by adding a blank line at the end). Rebuilding any code seems to trigger rebuilding all the asset files. The question is old, so maybe the behavior is different in more recent ADT builds (I'm on 20). – James Moore Aug 22 '12 at 19:25

6 Answers6

8

If i have understood your problem correctly, you are trying to alter the file in /assets folder. changing .apk contents after signing the package is not possible.

If the file is small, copy it into your app's private data directory.

If the file is bigger, copy it into the /sdcard.

Samuel
  • 9,883
  • 5
  • 45
  • 57
  • 1
    @sthompso what Sam says is right. The files that you store in the assets are read only. – AjOnFire May 06 '11 at 08:34
  • sthompso (who I work with) changed the asset file by dragging a new file into the Assets directory in the Eclipse project explorer, then rebuilding, then repushing. Doesn't rebuilding re-sign the application, or does it always have the same signature? – Ellen Spertus May 06 '11 at 20:24
  • @espertus, While the above is true, it doesn't answer the OPs question. It doesn't have anything to do with that problem. Re-installing the apk, should in fact replace the assets of the old apk with the ones from new apk, just like it replaces the actual app. – aph May 10 '11 at 19:52
  • @espertus & @sthompso, if you are having refreshing problems in emulator one way is to clean the data files under Manage Application or you can un-install the app and try re-installing. This may happen when eclipse trying to do code "Hot Swapping", and the asset file may not be considered as code change. – Samuel May 11 '11 at 00:52
  • 1
    This isn't with the emulator; it's with the actual phone. Thanks, though. – Ellen Spertus May 11 '11 at 01:17
  • This may happen in phone as well, if eclipse is 'Hot Swapping' the code, a change in asset file may not trigger a re-compilation as it is not a code change. – Samuel May 11 '11 at 01:34
3

It was happening to me with a .sqlite file in the assets folder and I solved it by

  1. uninstalling the app from the phone
  2. and rebuilding.
fersarr
  • 3,399
  • 3
  • 28
  • 35
2

What's the file size of databaseeleven.txt?

There is a limit of 1MB per file for the assets file, if the file size exceeds this limit it won't be available in your apk.

If this is your case, there are two alternatives I know of:

  • Split your file into 1MB chunks, you have an example this in this SO question
  • Use a file extension that doesn't get compressed by Android, as suggested here. Note that your file won't be compressed and you will probably end up with a big apk.

NOTE: It seems that the 1MB limit was removed in Android 2.3, so this only applies to 2.2 and lower.

Community
  • 1
  • 1
aromero
  • 25,681
  • 6
  • 57
  • 79
0

Clean the project after you edit the file....hope this helps

droid kid
  • 7,569
  • 2
  • 32
  • 37
0

hi try after refreshing the project in eclipse and clean & build it again. Hope you will be able to find the changes reflected in the emulator

Dinash
  • 3,027
  • 4
  • 32
  • 45
0

If its a txt file of your format, you should be doing something like this

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

where "options" is options.txt file in the ~/res/raw folder.

Any changes to this file will still require a publish/deploy back to the device/emulator so it has the latest apk.

Hope this helps...

Marty
  • 2,965
  • 4
  • 30
  • 45