-1

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;
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Cristina99
  • 21
  • 1
  • 7
  • If you have a copy of your file at assets folder, it will be addressed by `file:///android_asset/pays_names.json`, but I don't know if your already built method will read it correctly. – statosdotcom May 20 '18 at 02:06
  • where i can put this ? – Cristina99 May 20 '18 at 02:21
  • This is the question. Using your method, this would be referenced on your InputStream. I don't know if this will work. Maybe you will have to figure out another method to read. One thing is fact and you can count on it: `file:///android_asset/pays_names.json` is the address of the file on your assets folder (I use this address to load offline webpages on webViews and pdfs on pdfViews). Oh: you don't have to say thanks :D – statosdotcom May 20 '18 at 02:27
  • Why would you want to do this? If the file contains what its name suggests you may want to have language specific variants and these are easier to handle in the resources than in the assets. – Henry May 20 '18 at 07:18
  • `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 ? ` Those are two things. Do you really not know how to put a file in assets? Please be clear. – greenapps May 20 '18 at 07:46
  • @greenapps i mean , how to call it from the asset instead of raw folder ! – Cristina99 May 20 '18 at 09:18

2 Answers2

0
final InputStream inputStream = resources.openRawResource(R.raw.pays_names);

If the file is in assets then simply replace by

InputStream inputStream = getAssetManager().openInputStream("pays_names.json");

Why final?

greenapps
  • 11,154
  • 2
  • 16
  • 19
0

Thank you guys , i found the solution, i had to change only some line :

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());
}
}

with this one

public void load(Context context) {

    AssetManager assetManager = context.getAssets();
    try {
        InputStream is = assetManager.open("pays_names");
        final BufferedReader reader = new BufferedReader(new InputStreamReader(is));


        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());
        }
    } catch (IOException e) {

    }
}
Cristina99
  • 21
  • 1
  • 7
  • `, i had to change only some line :` Yes you had to change only one line. And i had already told you how to do that. Sorry for the typos... I did it from memory. Strange you did not realise that you got the answer. – greenapps May 20 '18 at 13:01