Setting custom name for raw resource - android

There are a couple of audio mp3 files in res/raw which go as prepacked audio files for the app. They all have proper names ...must contain only [a-z0-9_.] which do not look nice when parsed inside ListView.
I see many apps have their custom names for audio. For example, raw file name is "default1" and custom audio name is seen as "The Morning Shine".
How can I set custom name for all custom audios in res/raw?

If these files are static and prepacked as you say, try having an xml resource file that holds custom Strings for each file.
I don't know how you are managing your List, but with a custom Adapter you can link the proper name based on the resource ID.
Other way that maybe could help you is read the Tags directly from the files, with the Class MediaMetadataRetriever.
Example:
MediaMetadataRetriever retriever=new MediaMetadataRetriever();
retriever.setDataSource(yourFilePath);
String songName=retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
You have other constants like METADATA_KEY_ARTIST, METADATA_KEY_ALBUM and so on for the other fields.
See: http://developer.android.com/intl/es/reference/android/media/MediaMetadataRetriever.html
Don't forget to call retriever.release() when you're done to free memory.
Good luck.

Related

How to scan raw resources?

Is it possible to scan the raw resource folder and obtain a list of all the files in it? Or do I have to list all the files I want to use explicitly in my App?
Therefore, is the only possibility to access raw resources with
R.raw.<identifier>
or is it possible to somehow obtain a list of everything in R.raw.*?
I don't think there is a way to list down files from raw resource folder. But if you know the filenames, you can fetch from the identifier as:
int imageResource1 = getResources().getIdentifier("image1", "raw", getPackageName());
imageView.setBackgroundResource(imageResource1);
Where "image1" is the name of some png image file inside Raw folder.
See if this works for you assuming that you have serialised naming to your files, for example, image1.png, image2.png, etc

Android Studio:Where to store a text file and the path for it

I'm developing a strategy game that will have a country full of kingdoms. I want to be able to store and read back in the information of the kingdomgs. I've looked at tutorials online but they just aren't specific to what I'm looking for. So basically:
-Where to store a text file which holds string values.
-The correct file path.
-And how to read from that text file, and check if it is empty.
You can store text data in string resource file:
https://developer.android.com/guide/topics/resources/string-resource.html
Also you can use asset to store text
https://developer.android.com/reference/android/content/res/AssetManager.html
And there is diffrenece:
Difference between /res and /assets directories

How can I scan through a raw text file?

I have text files in the raw folder that I would like to use in my app. I am using ListFragments and depending on what item is selected I want to load that files text in a new layout.
I can properly load the text fine but I would like to be able to scan the file previously so that I can have the first line of the file be the title that is displayed in the ListView. I have a method that determines the number of files in the raw folder and adds the names to the ArrayList.
Here is my method:
private void listRaw() {
Field[] fields = R.raw.class.getFields();
for (int count = 0; count < fields.length; count++) {
myArrayList.add(fields[count].getName());
}
}
The problem i am having is setting up a scanner. I do not know how to tell the scanner to scan this specific file. I tried making a new File object and used the Scanner(File) but I do not think I am declaring the file name properly nor if this is even the best way to get this done. Normally if you know the file name you can just simply do:
Scanner fileReader = new Scanner("filename");
but in this loop I never actually have the file name set.
I have text files in the raw folder that I would like to use in my app
I am assuming that "the raw folder" means the res/raw/ resource directory in your project.
I do not know how to tell the scanner to scan this specific file
That is because there is no file. The file exists on your development machine. The representation of the raw resource at runtime is just that: a raw resource. Under the covers, it's effectively an entry in a ZIP file.
You need to get the R.raw value for a raw resource, then use getResources().openRawResource() to get an InputStream that you can pass to a Scanner.
Unless you have different editions of these raw resources for different configurations (e.g., language), you might find it easier to work with the assets/ directory and AssetManager for packaging text files with your app.

Where are the resources' IDs?

Android will allocate ids for each pics in the res/drawable directory. Where are they?
I want to dynamically choose one pic form the pool and show it. How can I do that?
They are stored in the res/drawable folder.
If the file name is demo.png, then they can be accessed by R.drawable.demo
If you want to access a random drawable, store all the resource identifiers in a Integer ArrayList, and programatically generate a random function using Random(), and get that particular item from the arraylist. Then you'll have a random drawable every time.
Autogenerated ids are in the gen file, but not advisable to use them. It would be better to use the filenames directly through some predefined array of R.drawable.filename and randomly pick them.
There IDs are stored in the R.java file, but you cannot edit it, as your changes are over written each time.
You can also access resources by name, which may be a viable approach to solving your problem if you know the names of the resources or can derive them according to some pre-defined naming scheme. (for example images are named in the sequence image1, image2 and so on.
You have to map the name to the identifier using the getIdentifier() method of the Resources class.
String name = "resource" + rng.nextInt(count);
int resource = getResources().getIdentifier(name, "drawable", "com.package");
The documentation for this method says:
Note: use of this function is
discouraged. It is much more efficient
to retrieve resources by identifier
than by name.
This is true but need not be a problem if you are doing it in code that isn't performance sensitive.
Alternatively, if you don't mind listing the resources in XML, you could create a typed array that you can then randomly select from.

Android R.java use

When we add some entries in the strings.xml file or layout.xml file, then the R.java file gets modified automatically. Again, if we want to refer something from the layout file such as reading the EditText value entered by the user, then again we refer the R.java file at our java code to read the values.
What is this R.java file all about? The values of each entry of this R.java file seems to be in HEXADECIMAL format but what is its use?
I have read the doc but i get fairly confused for this R.java :(
Please someone step forward and explain what is this R.java file all about :(
Regards,
http://developer.android.com/guide/topics/ui/declaring-layout.html says:
android:id="#+id/my_button"
The at-symbol (#) at the beginning of the string indicates that the
XML parser should parse and expand the rest of the ID string and
identify it as an ID resource. The plus-symbol (+) means that this is
a new resource name that must be created and added to our resources
(in the R.java file).
The R.java file is generated by the Android Resource Manager (aapt.exe) and contains references to all resources of your app. Each reference is a unique id (public static final int). These constants are written to the R.java file in hexadecimal format. The logic of assigning specific integer to each resource is private to the Android Resource Manager. You can look at the source code of aapt.exe on the internet, e.g. at http://gitorious.org/rowboat/frameworks-base/trees/d58fb97ddf052b3ceac921ac7e936af990392b2c/tools/aapt
It turns resource objects into Java recognizable names for you to refer to in your code.
The R class is basically the way Android provide resource access from within your code. As you wrote when you alters strings.xml etc on save of that resource file Android SDK will recompile the R class to make your changes accessible for within your code. What the values in R class are is more or less not important since its used by Android internally to map, for example a string to an ID.
To reference a string from within your code you use R like this:
R.string.MyString
If you string in string.xml is called MyString. Same for layouts etc.
I guess this is what you read, but otherwise it's pretty good explained here.

Categories

Resources