i have a file in raw folder , and i want to put it inside assets folder and load it from.
how can i do that ? here is my code and file name is pays_names.json.
public static CountryFlagsLoader getInstance() {
return ourInstance;
}
public void load(Context context) {
/// load file from raw folder
Resources resources = context.getResources();
final InputStream inputStream = resources.openRawResource(R.raw.pays_names);
final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
Gson gson = new Gson();
Type collectionType = new TypeToken<Collection<Country>>() {
}.getType();
Collection<Country> countries = gson.fromJson(reader, collectionType);
for (Country country : countries) {
countryToCode.put(country.getName().toLowerCase(Locale.ENGLISH), country.getCode());
}
}
public Drawable getFlag(Context context, String countryName) {
String countryCode = countryToCode.get(countryName.toLowerCase(Locale.ENGLISH));
if (countryCode != null) {
Resources resources = context.getResources();
final String resourceName = "flag_" + countryCode.toLowerCase();
final int resourceId = resources.getIdentifier(resourceName, "drawable",
context.getPackageName());
if (resourceId != 0) {
return resources.getDrawable(resourceId);
}
}
return null;
}