Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning : Do not hardcode "/data/"; use Context.getFilesDir().getPath() instead

I developed an application in which I copied the database from the assets folder to my path which is hardcoded. So eclipse gives me warning :

Do not hardcode "/data/"; use Context.getFilesDir().getPath() instead

I searched in google and found the answer as to use :

Context.getFilesDir().getPath();

And the hard coding is not working on every device, on some it may give an error or not work properly. But by implementing the above i am getting error.

My code is as follows :

private final Context myContext;

Getting warning here

private static String DB_PATH =  "/data/data/com.example.abc/databases/";


private static final int DATABASE_VERSION = 1; 
private static final String DATABASE_NAME = "exampledb.sqlite";
static SQLiteDatabase sqliteDataBase;

public DataBaseHelperClass(Context context) {       
    super(context, DATABASE_NAME, null ,DATABASE_VERSION);
    this.myContext = context;
}

public void createDataBase() throws IOException{
    boolean databaseExist = checkDataBase();

    if(databaseExist){
        this.getWritableDatabase();
    }else{
        this.getReadableDatabase();         
        copyDataBase(); 
    }
} 


public boolean checkDataBase(){
    File databaseFile = new File(DB_PATH + DATABASE_NAME);
    return databaseFile.exists();        
}

private void copyDataBase() throws IOException{ 
    InputStream myInput = myContext.getAssets().open(DATABASE_NAME); 
    String outFileName = DB_PATH + DATABASE_NAME; 
    OutputStream myOutput = new FileOutputStream(outFileName); 
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }

    myOutput.flush();
    myOutput.close();
    myInput.close(); 
}


public void openDataBase() throws SQLException{      
    String myPath = DB_PATH + DATABASE_NAME;
    sqliteDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);  
}

@Override
public synchronized void close() { 
    if(sqliteDataBase != null)
        sqliteDataBase.close(); 
    super.close(); 
}

public Cursor myFunction(){

}

@Override
public void onCreate(SQLiteDatabase db) {}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {} 

Please suggest me the way how to resolve warning.

like image 730
Manoj Fegde Avatar asked Dec 05 '13 04:12

Manoj Fegde


1 Answers

The tip that eclipse gives you is not good enough.

You can get database path with context.getDatabasePath(); You should pass the desired name to the file (no matter if it exists or not), in your case exampledb.sqlite

So your code will be:

File outFile =myContext.getDatabasePath(DATABASE_NAME);
String outFileName =outFile.getPath() ;

of course, myContext has to be the current context. This is for instance the running activity or service that is calling this.

like image 115
Carlos Robles Avatar answered Sep 27 '22 23:09

Carlos Robles