AssetManager in android
Basically, Assets are a raw approach to resource management. We can place any files we want into the assets directory of our project.
If we have a reference to any Context subclass, such as an Activity, we can get a reference to the AssetManager instance provided by the platform:
- AssetManager assetManager = getAssets();
Once we have an AssetManager reference, we can just open a raw InputStream for any asset we put into the assets directory
Like in this way:
- <> InputStream input = null;
-
- <> try {
-
- <> input = assetManager.open("mytextfile.txt<> ");
-
- <> } catch (IOException e) {
-
- <> // handle
-
- <> }
Here is an example as a class having feature of text file reading via AssetManager
For instance
- public static class myData
- {
- public static void loadData(Context context)
- {
-
- try
- {
- AssetManager am = context.getAssets();
- InputStream is = am.open("mytextfile.txt");
- BufferedReader br =
- new BufferedReader(new InputStreamReader(is));
- String line = br.readLine();
- My_Name = line;
- line = br.readLine();
- My_Contact = Integer.parseInt(line);
- br.close();
- }
- catch (IOException e)
- {
-
- Log.e("Error", "Unable to read data from txt file");
-
- }
- }
- }