access resources of an apk - android

I'm thinking about putting a .txt file in res/drawable or in res/layout to have it in the apk when the application is packed, and then after install to open and read from it to perform some tasks. Can this be done? If yes please let me know how can I access the file, as I don't know it's path.

A .txt file is neither a drawable nor a layout. You should put it in res/raw. You can get access to it like:
Resources resources = this.getResources();
InputStream is = resources.openRawResource(R.raw.yourtextfile);
Try/catch and errorhandling omited in above code.

Related

How to create a folder programmatically in Android

I wonder how you (C#)programmatically create a folder in the Assets Folder in Android.
My project is called App1 so the folder I want to create is named: "imagefolder1"
The full path look like this:
App1/Assets/imagefolder1
How can we create "imagefolder1" here?
(Notice that I want to have many image folders in the Assets Folder. Please tell if this is the wrong place to have them)
I wonder how you (C#)programatically create a folder in the Assets Folder in Android
You cannot do this, in any programming language. Assets are not files on the device. They are packaged in the APK, and you cannot modify them, including creating directories.
var pathToNewFolder = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/Assets/imagefolder1";
Directory.CreateDirectory(pathToNewFolder);

Distribute an xml file with my Android app

I have created a java application using "Simple" that allows me to serialize objects in some configuration xml file.
These files must be redistributed with my android application, but here comes the problem:
Where do I place the files? Is it better to place them in the res/xml folder or create a custom path to the assets folder (assets/myfiles)?
In the examples given with Simple if I want to deserialize an object I need an xml file:
File fl = new File ("string to file path");
but if the file is in the resources of my project, how do I get the file?
Searching the internet I found only examples that use a XmlResourceParser but I don't know how to use it with Simple!
If you are going to include files you can later access from your application you'll need to put them in the assets folder. After your application has been installed and running you can get get that xml file by using the AssetManager class:
AssetManager assets = getAssets();
InputStream stream = asset.open("filename");
That will give you an InputStream for you to do whatever you need.

Access to resource folder route

I'm developing a test class that have to check if there are all the resources. It's a specification mandatory that i access to the resources by file name (png's).
I know there's the possibility of loading as a resource, but I cannot load resources. I'm developing a test app. How I could access the file file.png in res/drawable named ?
new FileOutputStream("/res/drawable/xxx.png");
It is recommended to read Resources Overview from Android docs,but if your problem is only about accessing resources,read Accessing Resources.So you can do what you want.
Summery,there is some ways to access resources that depends on situations,for example when you know name of resource or you know it's ID.And also it depends that where you want to access resources.
In your case I guess that you want to access a drawable in your code,if it is true,you can do like this:
R.drawable.xxx
For example if name of your .png file is myimage and you have to set it in ImageView with name im do this:
ImageView im = ...;
im.setImageResource(R.drawable.myimage);
Edit:
If you have to get an InputStream form a file in resources,you should consider creating a raw folder inside res directory and put your file in it and then call getResources().openRawResource(resourceName) from your Activity or widget.For example :
InputStream ins = getResources().openRawResource(R.raw.test);
Here your file that is in raw folder is for example test.png.
You can see more details here:
Using files as raw resources in Android
Android: Loading files from the Assets and Raw folders
stackoverflow
stackoverflow
stackoverflow
Finally if you want to create an output file from your resource,get an InputStream from it and write it's content to an OutputStream,you can see a good example here.

where to store XML files

I'm storing app data in a few XML files. Should I put these XML files in the 'res' folder? Is there anything special I need to do besides just using something like:
XmlPullParser xpp = getResources().getXml(R.xml.myXMLfile);
Thanks
I would suggest to store these file in the assets folder. Use the following code to read file form the assets folder:
getResources().getAssets().open(fileName);
No, nothing, as long as these are completely static in nature.
If it is an app data & if you feel to have lightweight app then try to store in the internal memory ,make use of it & get benefited.

How to create new XML resource ids at runtime?

I want to create new XML resource IDs at runtime. Just generic XML resources like in the res/xml folder, but from a file on an SD card or anywhere in the filesystem.
I found this solution that says it's possible: How do I generate XML resources at runtime on Android?.
It says to:
Save XML into another external folder (SD card or so)
Create AssetManager over those file(s)
Then create Resources which can be later used as ordinary Android resource stored in APK
I don't understand step 2 since there is no constructor for AssetManager. How do I create an AssetManager for external files?
Thanks in advance,
Barry
By create, I think they mean get the AssetManager:
Context.getAssets()
Then, use the asset manager to open the XML file:
AssetManager.openXmlResourceParser(String fileName)

Categories

Resources