I have a file named counter.txt in the assets folder of my Android project. At runtime I want to overwrite the content of the file. But it's not reflecting. How can I fix this problem?
I have used the below code.
String FILENAME = "counter.txt";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
As far as I know, you can't write/update in the assets folder. The only thing you can do is read.
More precisely, you can't make any changes to the 'asset' folder at runtime through your code, but after you compile your code and get the APK file, you can unzip it, change the contents of the 'asset' folder and zip all the files to APK again.
However, you must sign the APK file again to make it work.
I think you should use class SQLiteDatabase in such a case to store all your confidential but frequently updated counters.
Related
I have an app which has a .db (sql database) inside the "assets" folder.
When I need to update this database, I simply copy the new version in the "assets" folder, change the user version of the .db file and rebuild the apk. At runtime the application deletes the file in memory and copy exactly the one in the "assets" folder, then it checks if the version of this file is greater than the last one it had in memory (in another database) and, in that case, it does some stuff (which I'm skipping, since is not the point of the question).
The problem is: my application seems to ignore this new file, maintaining the old file in the "assets" folder, so naturally no change is detected.
It seems like my new file is not present in the apk, but only in devices with Android 9.
Is something changed with the usage of the assets folder that I am missing?
How can I arbitrarily update a file inside my apk when packing and upgrade?
This exact process works fine for older devices, then I had to upgrade the target sdk version to 28 and I got this problem while testing on an Android 9 (Pie) device.
Here I delete older file:
File l_DatabaseDir = p_Context.getDir("Database", Context.MODE_PRIVATE);
File l_DatabaseFile = new File(l_DatabaseDir, "MainDatabase.db");
File l_PlayersFile = new File(l_DatabaseDir, "players.db");
l_PlayersFile.delete();
Here I copy the file from the assets folder:
try
{
InputStream inputStream = p_Context.getAssets().open("players.db");
//Create the file before
OutputStream outputStream = new FileOutputStream(l_PlayersFile);
byte[] buffer = new byte[4096];
int length;
while ((length = inputStream.read(buffer)) > 0)
{
outputStream.write(buffer, 0, length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
} catch (IOException e)
{
e.printStackTrace();
}
Finally I try to get the current version of the db:
m_PlayersDatabase = SQLiteDatabase.openDatabase(l_PlayersFile.toString(), null, SQLiteDatabase.OPEN_READWRITE);
int playersVersion = m_PlayersDatabase.getVersion();
Result on android 9: db version is the same as the first apk installed
Result on android 8: db version is the one of the file included in the new apk
EDIT: after further investigating it seems that the problem is more related to android memory, because simply changing the database file name (basically the destination of the copy)
l_PlayersFile = new File(l_DatabaseDir, "newplayers.db");
makes the application read the correct file also in android 9, so it looks like maintaining the same file name as the old one is causing the problem (Even if both the deletion and the copy of the new file are completed successfully).
So the assets folder update is fine.
The issue is only postponed to the next update though, since using again the name "newplayers.db" causes the exact same problem.
Generating a random name at every opening is quite brutal as workaround, any ideas?
Solved as I stated in the edit: copy the file from the assets folder with a different name (in my case with a timestamp).
You can change the assets folder's name eg:dist->dist1,then assets will be upgraded.
How do I delete a file or folder on the internal storage in android with Delphi for android
You should use the TFile and TDirectory classes in the System.IOUtils unit.
For example:
TDirectory.Delete(<YOUR DIR PATH>);
or
TFile.Delete(<YOUR FILE PATH>);
Look in Embarcadero's documentation to get the right path of your files and folders on the various platforms:
Standard RTL Path Functions across the Supported Target Platforms
Referred from :
https://stackoverflow.com/a/27047502/5193608
https://stackoverflow.com/a/3554949/5193608
Main Part from both links:
You should always delete files that you no longer need. The most straightforward way to delete a file is to have the opened file reference call delete() on itself.
myFile.delete();
If the file is saved on internal storage, you can also ask the Context to locate and delete a file by calling deleteFile():
myContext.deleteFile(fileName);
Note: When the user uninstalls your app, the Android system deletes the following: All files you saved on internal storage All files you saved on external storage using getExternalFilesDir(). However, you should manually delete all cached files created with getCacheDir() on a regular basis and also regularly delete other files you no longer need.
Source : http://developer.android.com/training/basics/data-storage/files.html
Directly you can do is :
File dir = getFilesDir();
File file = new File(dir, "my_filename");
boolean deleted = file.delete();
Hope,it helps!
I need to work with a file,that should be at data\data\app_name\files\ . That is now an SQL Database, so solution with SQLiteOpenHelper isn't ok for me. Also, I copyied that file to app_name\app\assets in my profect folder - that didn't work.
How to add that file to installation .apk, to put it in data\data\app_name\files during the installation?
Save your file in \res\raw within your project.
Then, you can access that file using:
InputStream databaseInputStream = getResources().openRawResource(R.raw.yourfile);
Make sure that the resource files are included correctly when generating the apk.
I'm programming an app, which needs to store data in a textfile.
I'm using FileOutputStream:
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(data.getBytes());
outputStream.close();
My question is: Where is the file saved?
I can't find it anywhere...
For debugging, I'm using a nexus7.
It's stored under /data/data/<your app> by default. But unless you have rooted device, you have no access to this folder from device. But you can take a look on your file by using DDMS from the Android SDK (at the android-sdk\tools folder)
If You are using Context.MODE_PRIVATE it is stored in application data directory and you do not have access to them from file manager
/data/data/your.app.package.name/files, but note this can be device dependent.. you can find it in eclipse by typing DDMS and then go to file explorer and then follow the above path
writes to a file in your internal directory
/data/data/pacakage/files
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.
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.
It was happening to me with a .sqlite file in the assets folder and I solved it by
uninstalling the app from the phone
and rebuilding.
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.
Clean the project after you edit the file....hope this helps
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
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...