javaandroidandroid-studioandroid-assetsimport-from-csv

AssetManager doesn´t open a CSV file in folder "assets"


I am sorry for asking this but I am going to go crazy trying to solve it.

I call a method from my activity

d.loadQuestions(this);

I use that method to import two CSV files to a SQLite table.

public void loadQuestions(Context context){
        if(thereAreNotPacks()){
            SQLiteDatabase db = this.getWritableDatabase();

            String mCSVfile;

            switch (Locale.getDefault().getLanguage()){
                case "es":
                    mCSVfile = "English-Spanish.csv";
                    break;
                default:
                    mCSVfile = "English-Spanish.csv";
                    break;
            }

            String COL1 = "QUE_QUE";
            String COL2 = "ANS_QUE";

            AssetManager manager = context.getAssets();
            InputStream inStream = null;
            try {
                inStream = manager.open(mCSVfile);
            } catch (IOException e) {
                e.printStackTrace();
            }

            String currentPlayer = ClassSharedPreferences.read(ClassSharedPreferences.currentPlayer, "");

            BufferedReader buffer = new BufferedReader(new InputStreamReader(inStream));
            String line = "";
            db.beginTransaction();
            try {
                while ((line = buffer.readLine()) != null) {
                    String[] colums = line.split(";");

                    ContentValues cv = new ContentValues();
                    cv.put(COL1, colums[0].trim());
                    cv.put(COL2, colums[1].trim());
                    cv.put("PRI_QUE", 1);
                    cv.put("PAC_QUE", "English-Spanish");
                    cv.put("PLA_QUE", currentPlayer);

                    db.insert(MI_TABLA_QUESTIONS, null, cv);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }


            switch (Locale.getDefault().getLanguage()){
                case "es":
                    mCSVfile = "Spanish-English.csv";
                    break;
                default:
                    mCSVfile = "Spanish-English.csv";
                    break;
            }

            try {
                inStream = manager.open(mCSVfile);
            } catch (IOException e) {
                e.printStackTrace();
            }


            buffer = new BufferedReader(new InputStreamReader(inStream));
            db.beginTransaction();
            try {
                while ((line = buffer.readLine()) != null) {
                    String[] colums = line.split(";");

                    ContentValues cv = new ContentValues();
                    cv.put(COL1, colums[0].trim());
                    cv.put(COL2, colums[1].trim());
                    cv.put("PRI_QUE", 1);
                    cv.put("PAC_QUE", "Spanish-English");
                    cv.put("PLA_QUE", currentPlayer);

                    db.insert(MI_TABLA_QUESTIONS, null, cv);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            db.setTransactionSuccessful();
            db.endTransaction();
        }
    }

Everything is fine. The first CSV file (English-Spanish.csv) is imported correctly, but there is some problem with the second. The second CSV file (Spanish-English.csv) cannot be found I don´t know why. The next line works to find the first CSV file perfectly, but the same line doesn´t work with the second file. How it is possible?

inStream = manager.open(mCSVfile);

When I debug it and I reach that line for the second CSV file, it takes null value. I get this logcat:

2021-08-15 19:31:42.647 10083-10083/com.doctoractual.pregresp W/System.err: java.io.FileNotFoundException: Spanish-English.csv
2021-08-15 19:31:46.720 1550-1586/? W/EntropyMixer: Failed to add HW RNG output to entropy pool
    java.io.FileNotFoundException: /dev/hw_random: open failed: EACCES (Permission denied)
        at libcore.io.IoBridge.open(IoBridge.java:496)
        at java.io.FileInputStream.<init>(FileInputStream.java:159)
        at java.io.FileInputStream.<init>(FileInputStream.java:115)
        at com.android.server.RandomBlock.fromFile(RandomBlock.java:45)
        at com.android.server.EntropyMixer.addHwRandomEntropy(EntropyMixer.java:205)
        at com.android.server.EntropyMixer.access$000(EntropyMixer.java:59)
        at com.android.server.EntropyMixer$1.handleMessage(EntropyMixer.java:83)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.os.HandlerThread.run(HandlerThread.java:67)
        at com.android.server.ServiceThread.run(ServiceThread.java:44)
     Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
        at libcore.io.Linux.open(Native Method)
        at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
        at libcore.io.BlockGuardOs.open(BlockGuardOs.java:252)
        at libcore.io.IoBridge.open(IoBridge.java:482)
        at java.io.FileInputStream.<init>(FileInputStream.java:159) 
        at java.io.FileInputStream.<init>(FileInputStream.java:115) 
        at com.android.server.RandomBlock.fromFile(RandomBlock.java:45) 
        at com.android.server.EntropyMixer.addHwRandomEntropy(EntropyMixer.java:205) 
        at com.android.server.EntropyMixer.access$000(EntropyMixer.java:59) 
        at com.android.server.EntropyMixer$1.handleMessage(EntropyMixer.java:83) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.os.HandlerThread.run(HandlerThread.java:67) 
        at com.android.server.ServiceThread.run(ServiceThread.java:44) 
2021-08-15 19:32:09.752 10083-10083/com.doctoractual.pregresp W/System.err: java.io.FileNotFoundException: market_spanish.csv

Both files are in the "assets" folder and "assets" folder is in "main" folder. So I really cannot understand why the app can find the first file and it cannot find the second one.

enter image description here


Solution

  • I found the problem and I post here the solution.

    I was always adding the files to asset folder using Windows file explorer, and it worked.

    After the last update of Android Studio, or maybe I changed some setting, the only way to make it work is adding the files through Android Studio, using the "Project" tab.

    That was the simple problem finally. I hope it is useful for some else.