Why aren't my asset files updated? - android

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...

Related

Android studio, pass assets file path to another method

I am making an app which use the following java line:
Net dnnNet = Dnn.readNetFromONNX("path\to\file");
As you can see, readNetFromONNX requires path to onnx file.
So I put my onnx file under assest folder, so I can use it at run time.
The problem is I you can only read assets foler with AssestManager and inputstream... the readNetFromONNX needs path...
How do I overcome this probelm? Is there a way to get the path of the file at run time? Maybe other folder?
Thanks from advance

Assets folder not upgraded when installing new app

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.

Get config.xml file of Cordova applications using Androguard

Is it possible to get the config.xml file that is generated by Cordova based applications using Adroguard?
I cannot find it under the /res folder and it is not listed in the output of get_files() method.
apktool, on the other hand, is able to get the config.xml from the apk that is use it on.
Since it is under res folder, You need to get it by unzipping the file to a temporary directory and then parse it using Axml parser. In get_files(), you must see "resources.arsc" file. The res files are part of that. You can do something like :
config_xml_path = os.path.join(<your apk unzipped path>, 'res', 'xml', 'config.xml')
with io.open(config_xml_path, 'rb') as axml_file:
parsed_axml_file = AXMLPrinter(axml_file.read())
axml_content = parsed_axml_file.get_buff().decode('utf-8)
parsed_axml = minidom.parseString(axml_content.encode('utf-8'))
You might get some errors if the config.xml is badly formatted but I am not including the solution to handle those case. I hope you will get an idea with the above example.

Cannot copying file from Android to Linuxmint 17.3 Desktop "libmtp error: Could not get file from device"

I am trying to copy image file to backup it on my desktop which is linux mint (17.3 or Rosa) installed. I am using Infinix X510 phone with the newest version of android 6. At the beginning it was OK. But, at the end i get the message
“libmtp error: Could not get file from device”
I tried the solution as found by Googling here https://askubuntu.com/questions/879029/libmtp-error-could-not-get-file-from-device-on-attempting-to-transfer-files
But, it wasn't working for me. Is there a way to solve this matter? I need file to support my company job which supplying cosmetics.
Try compressing the file to a compressed format(tar,zip).Copy the file and then decompress it.
Changing the filename was the solution for me e.g: file.mkv to file.zip
Try to rename file, make sure file name not contain (-) (:) etc. Just rename it to alphabet

Put an existing file to data\data\app_name\files during .apk installaion

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.

Categories

Resources