简体   繁体   中英

False returned for File exists(); when it does Android internal storage

Inside a Fragment

    SQLiteDatabase savedDutiesDB = null;


    public void createDatabase() {

         try{


          savedDutiesDB = getActivity().openOrCreateDatabase("savedDuties",Context.MODE_PRIVATE, null);


          savedDutiesDB.execSQL("CREATE TABLE IF NOT EXISTS storedDuties " +
                                "(dutyBookName VARCHAR, dayOfTheWeek Int, pageNumber Int);");


          File databaseFile = getActivity().getApplicationContext().getDatabasePath("SavedDuties.db");
          Log.d("TCB","path:" + databaseFile);



   // Check if the database exists
       if (databaseFile.exists()) {
                            Log.d("TCB","database Created");

                        } else {
                            Log.d("TCB","Missing:" + databaseFile);    
                        }

                    }

                    catch(Exception e){

                        Log.e("TCB", "Error: "+e);

                    }


                }

Logs out

D/TCB: path:/data/data/com.domain.appName/databases/SavedDuties.db
D/TCB: Missing:/data/data/com.domain.appName/databases/SavedDuties.db

However the file does exist as I can see it in Android device monitor and I can write to it.

Why could this be returning false?

You are creating a database named "savedDuties" . You are trying to access the file for a database named "SavedDuties.db" . Those are not the same.

This might be due to Permissions.

This might also be an issue of a misconstructed file path, try using : databaseFile.getAbsoluteFile().exists() instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM