How can I scan through a raw text file? - android

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.

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

Can there be a content directory, where I can put files (compile time) and load files from (run time), inside an app?

I have some image files which will be dynamically loaded in my app. I know I can put images in the "/res/drawable" directory, but can there be somewhere else I can put files and loaded them by file name at runtime?
The image files contain characters that are now allowed in resource names. I did not name them, so ideally, I need to keep the names. For example, suppose I have "Cat #1.png" and "Cat #2.png", and I would like to load the image into an ImageView by that name at runtime, like so:
MyImageView1.setImageBitmap(createBitmapFromContentDirectory("Cat Pictures/Cat #2.png"))
MyImageView2.setImageBitmap(createBitmapFromContentDirectory("Dog Pictures/Dog #1.png"))
Is that possible, or should I change all image names into conforming names (e.g., cat_number1.png and cat_number2.png) and put them into the drawable directory?
You can put them in assets & load them from there,
Otherwise you can also put them in raw folder.
you can refer to Where do I I Place assets folder for assets.assets doesn't create Resource Id, so you can access them directly. Have a look at this
difference between assets & raw.

Setting custom name for raw resource

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.

Override res/values/strings.xml file with /strings.xml download to sd card from server android

I want to replace strings.xml file which is embedded into the apk, by a strings.xml file downloaded from server and stored into my internal sd card. Is it possible to reference external tags to TextView. My clients want to have complete control over languages in the application. If i successfully download strings.xml from server and stored it to sd card, how can i reference the tag element to respective TextViews. I don't really need code but only ideas and critism about this method.
I don't think this will work because your resources get compiled into the apk in a special way so that they can be accessed based on the device configuration. This happens at compile time. You can't simply swap out the strings.xml later while the app is installed.
This is not possible. If you want to achieve this, you will have to set the language in runtime while creating your controls.
If you have complete layouts (not creating dynamic controls), you will have to recursively scan your layouts, looking for buttons, textviews, hints etc., recognize the control and set the text property from the downloaded file.
something like this:
put your language in a hashmap:
HashMap<String, String> LANG = new HashMap<String, String>();
and set the tag property for each of your controls in your layout. Then you can find them like this:
for(int i=0;i<yourLayout.getChildsCount;i++)
{
View v = yourLayout.getChildAt(i);
if(v instanceof Button){
((Buttons)v).setText(LANG.get(v.getTag()));
}
}

How can I create an external shared pref/config file for my Android application?

I am looking for a way to store configuration setting on an external file on the sd card. The app must look into this external file and then retrieve some kind of settings. At the moment i am trying to get it to look into a file and get a name. I know you can store things in shared preferences but they are internally accessed.
Anybody know an external way? Thanks
The idea is to have a simple text file or xml file on the sd card. So when a configuration needs changing it is done thru that file?
EDIT
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"/Config.txt");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line="";
int c;
while ((c = br.read()) != -1) {
line+=(char)c;
if(String.valueOf(c) == ";" & line =="name;"){
String name ="";
}
}
}
I have attempted to read in a config.txt file and to separate each value with a ;
But i cant seem to understand exactly how i am going to attach what comes after name; to the variable String name.
the config.txt file has the following:
name;fred
somethingelse;test
The program should know when it has got to name and then set the name variable to fred??
Hi
I had same kind of preferences reading in Blackberry with (Comma separated or) semicolon separated values. When same development came to android the developers (I was not on android at that time.) This is what they have done.
We had created a text file like this
"name";"value";"data_type"
For example
"application_runs";"1";"int"
Or
"trial_period_key";"01ab23cd";"string"
The data type is hardcoded so we can detect the datatypes. For variable names we also had hardcoded list of preferences. And values can be parsed accordingly. They have written public readable shared preference based on that txt files, to use default values, they have copied a "default.txt" in assets folder along with a copy in SD card.
The drawback of this procedure is
You have to be careful about file name and the data written in that file (this is the reason why we had put a default values file in assets so the app doesn't crash)
The text file on SD Card must be readable, you have to program it along with parsing.
Hope it helps.

Categories

Resources