Access to resource folder route - android

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.

Related

Add XML Serialized object to APK so it is copied into the app's private files directory

I have a desktop application that produces resource / data files for my android app. These are XML text files that store instances of my custom data class. These objects are serialized using the Simple XML Serialization library. In my android app, I'd like to instantiate objects from this XML serialization class.
I like to add these xml files to Android Studio so they are included in the APK on device install and are placed, for example, in the private app directory "files", to which getFilesDir() is mapped. I can't find a way to do that.
If I add these xml files to the Android XML resource folder, I need to use Android's XML resource parser, and can not use the Simple XML library.
Any tips? I feel I made a wrong design choice seeing how restrictive the resource bundling is.
Thanks, Kind regards,
Harmen
As per CommonsWare's comment: the solution was adding it to the raw resource folder, then you can access it using:
InputStream xmlExerciseInputStream = getResources().openRawResource(R.raw.myresource);
MyClass myClass = serializer.read(MyClass.class, xmlExerciseInputStream);

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.

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)

Load and modify xml file in Android

I have an xml file which will be in either assets or res folder which i need to modify by adding some values and then i need to convert the xml into a string to be sent as part of a soap request. What would be the easiest way to achieve this?
You can't modify resource files.
If you don't need modify resources, you can put the XML file in assets folder. Then you can access it by AssetManager (returns by getResources().getAssets()). With AssetManager you can get InputStream from XML file or use XMLResourceParser. And do with it whatever you want.

access resources of an apk

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.

Categories

Resources