I want to obtain only the available languages for my android application, i.e. those for which I have a folder named values- (e.g. values-en, values-fr) in res folder.
I do not want to store the language codes and I think to list all the sub folders of "res" of the form values-* and take the language code from their name. (eventually check if the code is in the array returned by Locale.getAvailableLocales() to be sure that it is correct). This idea is stated here How to get the available languages (Not all of them, just the languages available on my app).
I have tried using
getResources().getAssets().list("res"); getResources().getAssets().list("/res");
getResources().getAssets().list("/res/");
but none of them worked.
Do you have any idea how I can list the sub folders of "res" folder?
Thank you in advance.
I don't know of a direct way to do this, but I have done a similar thing with the assets folder which should work fine. I use IntelliJ IDEA and ANT on Windows for my building, but you should be able to adapt this for Eclipse or other IDE and *nix/OSX.
Before compiling, I use an ANT build facet to run a dir command to list all files in the assets folder and pipe the output to a text file:
dir /assets /s /b > /res/raw/filelist.txt
I then read filelist.txt into a hashmap to give me very easy and fast way to find any file (I have hundreds of files in dozens of assets subfolders and AssetManager is too brain dead to deal with that).
public class AssetsFileManager {
private static Map<String, String> files = new HashMap<String, String>();
static{
BufferedReader in = new BufferedReader(new InputStreamReader(this.getResources.openRawResource(R.raw.filelist)))
String line = "";
while ((line = in.readLine()) != null) {
map.put(line.substring(line.lastIndexOf("\\")+1), line);
}
in.close();
}
}
Note the escaped backslash, \\
This gives me a map keyed on the filename and the full path as the value.
You could easily adapt this approach to get your folder list. E.g. you might use dir /ad /b /s
I'm not sure that this will work, but you can get all possible languages with Locale.getAvaliableLocales() and try to get some resource (e.g. app_name) for each locale in list. Here is one possible way.
Related
I am using Assets files as help files in my app and have well over a dozen. I am porting the app to multiple languages. Where do the alternative language asset files go?
I am already using the "res/values" directories for language files (values, values-es, etc) for use within the app. I thought the "Assets" directory was for help files and items like that.
I am trying to NOT muddy my values folders with the many help files that I am including and was using "activity.getAssets().open( file )" to read the files.
Also, some of these "Asset" files are different language pictures.
Can you put the files in /res instead of /assets? This has built in support for multiple languages, there is an easy to follow guide here.
Basically, if your original text is in /res/values/strings.xml, for example, you would put your translations in /res/values-{ISO LANGUAGE CODE}/strings.xml
For example, your French translation would be in /res/values-fr/strings.xml.
Android will pick the appropriate translation file according to the locale of the user's phone.
There are some good explanations of the other differences between /res and /assets here.
For the voice recognition in my app (using Vosk) I have defined the specific asset folder as resource string.
I.e. values\strings.xml contains:
<resources>
<string name="language_directory">vosk-model-small-en-us-0.15</string>
...
and values-de-rDE\strings.xml contains:
<resources>
<string name="language_directory">vosk-model-small-de-0.15</string>
...
So I can access the asset directory via
val assets = Assets(activity)
val assetDir = assets.syncAssets()
val modelDir = activity.getString(R.string.language_directory)
recognitionListener.model = Model("$assetDir/$modelDir")
This way the correct directory is always chosen based in the active locale.
In my case, those are located at models\src\main\assets\sync\<language_directory>
Make folder in assets:
1. htmlpagesNL
2. htmlpagesUS
Copy file from htmlpagesNL and paste to htmlpagesUS and Translate
Use url inside Nl string file:
file:///android_asset/htmlpagesNL for NL translation
Use url inside Us string file:
file:///android_asset/htmlpagesUS for US translation
Support different languages
Support different languages and cultures
I have large amount of files in a ZIP file (lets say 1000 images, some db files, binary files, ...). Inside it, I have some xml file I need to find and parse it. Information from it is shown to the screen. Problem is, when I am iterating through zip entry using:
InputStream inputStream = new FileInputStream(zipPath);
in = new ZipInputStream(inputStream);
for (ZipEntry entry = in.getNextEntry(); entry != null; entry = in.getNextEntry()) {
...some code here...
}
So when I am using Log.d, I see, it is iterating file by file, in case of large amount of files, it could take several minutes. Is there any better way, how to locate one specific file among others in ZIP file? "Brute force" approach I am using now is time consuming.
Thanks for any ideas
Waypoint
You can probably do this:
BufferedReader in = new BufferedReader(new InputStreamReader(zipfile.getInputStream(entry)));
When extracting specific files, you should be using a ZipFile. In particular, ZipFile.getEntry().
This should be no problem since you are using a File. However, if you only have an InputStream (or you prefer to use ZipInputStream for some reason) then, if you control how the zip file is built, you should put the XML file as the first entry.
I am new to Android development. I have an XML file with data that the app will read. Where should I keep this XML file? Should it be stored within the "value" folder?
I'd say that depends. What do you save in your XML-File? There also is a res/xml-folder, where XML-Files can be kept. But Android does nearly anything with XML-Files, so you might want to read my little Tutorial about where to put which recourses.
Also, there is a difference between the assets and the res-directory's:
res
No subdirectorys are allowed under
the specific resource-folders.
The R-class indexes all resources
and provides simple access.
There are some simple methods which
help reading files stored in the
res-directory
assets
Subdirectorys are allowed (as much as
you like).
No indexing by the R-class
Reading resources stored in assets
is done using the AssetManager.
You can put it in the res/raw folder. Then you will access it using:
getResources().openRawResource(resourceName)
I had a similar requirement and after lot of research , I found 2 solution to place a custom XML :
You can place custom XML in
res/raw/
res/xml/
To access these location you will use following code :
a. if XML is placed in res/raw then :
getResources().openRawResource(R.raw.custom-xml) :
This gives you easy methods for reading xml :
with below code I am reading XML in memory placed in raw folder :
BufferedReader br = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.custom-xml)));
StringBuilder str = new StringBuilder();
String line;
while ( (line = br.readLine()) != null){
str.append(line);
}
2nd Option :
getResources().getxml(R.xml.custom-xml);
with this you could read the xml using eventbased parser.
I need images to be sorted by folders. like drawable/Photos, drawable/items, drawable/photos. and put each folder to the registry or at least find way to get them out of there. all I want is something close to php like "../img/photos/photo0.jpg". is there way to do something like this. and folder with Images must contain in apk file.
possible solutions is make link in the R file but I didn't find how to do it, other solution is find command with logic like I will show you here:
ImageView img = (ImageView)CloningTable.findViewById(R.id.img);
String ImgPath = "com.test.test/img/img0.jpg";
img.setImageDrawable(Drawable.createFromPath(ImgPath));
OR
ImageView img = (ImageView)CloningTable.findViewById(R.id.img);
String ImgPath = "com.test.test/img/img0.jpg";
img.setImageResource(ImgPath);
please say me the best way to handle it. AND specify if it contain path how I can know path the file lies in.
File ImgFile = new File("test/test.jpg");
TextView testtext = (TextView)CloningTable.findViewById(R.id.testtext);
if (ImgFile.exists()) {
test.setImageBitmap(BitmapFactory.decodeFile(ImgFile.getPath()));
testtext.setText("asdasuidjasdk");
}
can any one say Why programm can't find file and file exist 100% = /?
filepath: Project> assets/test/test.jpg
found solution: Android Show image by path
It's hard to understand why you need to sort resources that will be static in your APK, but you should not access them directly using paths, but using the API for that purpose.
Take a look at the topics in the dev guide here:
http://developer.android.com/guide/topics/resources/accessing-resources.html
http://developer.android.com/guide/topics/resources/drawable-resource.html
Those will teach you how to access the resources. To create Bitmaps from them, the easiest way is to use the BitmapFactory class
Remember, you know which resources the APK has at building time, so you can work around it. If you want to work with Bitmaps created at runtime, then use the data storage methods instead.
In this case, you should put resources in assets folder, and you can access them by path!
So if you want to save the path string, the best way is creating a class, with 2 variants: 1 Bitmap to store data, and 1 String to store the path.
If not, just create a String array that you may access when you want.
In both case, you need to put the path into the Path String storage before you load the image.
Hope I understood your question.
EDIT: Final answer that meet your requirement:
There's no path for drawable, they are all converted into ID. If you put in asset folder, just call it by their name. For example, you have "Test.bmp" in asset folder, its path is "Test.bmp". If you have "SubTest.bmp" in "TestFolder" in asset folder, its path is "TestFolder/SubTest.bmp". Sorry for long time, I had to sleep, it was mid night at my time zone.
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.