Delete android file located in raw or assets directories - android

I make android application that read file.txt from resource/raw by context?.resources?.openRawResource(R.raw.file), and it's work fine.
I have tried assets directory by : context?.assets?.open("file.txt") and it's work also.
Now, I need to delete this raw/assets resource file or delete the content. Is there anyway to do it with android code?

No, there is no way to update the Assets or Raw files.
Both of them are read-only.
What you can do, it's to make a copy of the file in a writable directory and modify them as needed.
UPDATE
Just to give more context, both assets and any resources are part of the binary file (APK or app bundle). As they come as part of the package you cannot change them without creating a new binary.

Related

Pack the app with some files in the data->data->files directory

I have some files that I want to read from my app.Currently, I have to do the following:
Check if the file exists in the files directory.
If it doesnt, then copy the files from assets to the files directory
If it does, then, skip step 2
So, is it possible to not copy the files ? This increases the size of my app (uselessly) as there is an extra copy of all the files.
Note, that I have to access the file using getfiles(). I am using a library that doesnt work if I give the uri of my assets folder.
So, it it somehow possible to compile the app with some files already in the files directory ?
Internal storage for you app is created when application is installed so there is no way to provide files there during compile time, that would be magic.
You could try creating ContentProvider for sharing your files stored inside assets folder.
Besides, you should tell which library you are using. Then I may be able to suggest something more precise.

how to make copy asset folder in apk directory?

I am making hybrid application.I have one file in this directory assest->www->ATMS.html or other js files.So I load in web view like this.
web.loadUrl("file:///android_asset/www/a.html");
I want to copy all file files and folders which is inside the asset folder.Instead of loading from assest folder .I need to load from copy folder.As some some told me that there is file system of APK file.Mean we can copy all files in apk file system .and load html from that APK file system .
There is few link give information to copy of assest folder
How to copy files from 'assets' folder to sdcard?
But I don't get knowledge how to copy in APK file system .Mean when Aplication uninstall it remove the copied folder .When application launch it make copy of assest folder in apk file system ..
thanks
Not sure I get your question fully right as explanation is bit messy, but while APK is ZIP file, you should really consider it read-only. So you can copy files out of it but you cannot add anything to your APK nor alter it at runtime.

How to put extra files in Android APK with Eclipse?

I want to add a text file to my APK root. This file will not be used in the application but it will stay there for manual extraction of the APK.
I tried to put it into the root of the project in Eclipse but it didn't include that file in the APK. I don't want to put it into assets folder. Can't I put it to the root?
Use the assets folder.
According to Android developer
assets/
This is empty. You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.
Files that are not put into any of the main directories of your android project will NOT be included in your apk. You MUST put the file in the assets so it can be accessed within the application as an external resource/file.

How to add a folder to Android resources

I have a few thousand image files in a directory structure. There are a few hundred different subdirectories and each directory contains 10 images named 1.png - 10.png...
How can I add these folders to my app and access these images? Assuming I want foo/bar/1.png?
In android it doesn't appear like you have a string path to your assets or raw directories so I have no clue how I would do this. Please don't tell me I have to zip them then unzip them out of assets.
In android it doesn't appear like you have a string path to your assets
Sure you do. getAssets().open("foo/bar/1.png") will give you an InputStream to that PNG file, assuming that your project has assets/foo/bar/1.png in it. You can then use BitmapFactory to read it in.
However, resources cannot exist in subdirectories. You could write a script on your development machine that flattens the structure (e.g., foo/bar/1.png into foo_bar_1.png), then put the renamed files into whichever res/drawable-NNNN/ directory is appropriate.
the path to access the android asset folder is :
file:///android_asset/filename
You should be able to access a folder with :
file:///android_asset/folder1/folder2/targetFolder/
Note that there is 3 '/' after 'file:'

Configuring Android application from file

I am trying to create an Android application that would use a configuration file (plain text) when loading.
When testing locally (with an emulator), I place my config file in the src/ folder (ecplise) and I see it is copied and used from the bin/ folder upon project build.
My questions are:
1) Where do I need to place this file when testing on a device ?
From what I understand I need the *.apk file and the config file to be both present on the device during run time.
2) If I am using eclipse to install the *.apk file can I somehow specify the config file as a dependant file and force eclipse to copy it as well ? If not do I need to manually copy the *.apk & config files to the device ?
NOTE: I am using getResourceAsStream to load the file.
Thanks,
RM
Well actually there shouldn't be any problems with accessing the file in a way you do. But preferred (and recommended by Android developers) way is to place the file into /res/raw folder. Then you can access the file as application resource:
InputStream is = getResources().openRawResource(R.id.your_file_name);
Here getResources is a method of Context class, it should accessible anywhere within your activities. R.id.your_file_name is a static field generated by eclipse at the time you place your file into /res/raw folder.
Read the https://developer.android.com/guide/topics/resources/index.html to get more about application resources at Android.

Categories

Resources