17

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.

Manoj Fegde
  • 4,786
  • 15
  • 50
  • 95

3 Answers3

25

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.

Carlos Robles
  • 10,828
  • 3
  • 41
  • 60
  • 1
    I am getting while using your answer. I used your answer in constructor of database helper class and getting error as : nullpointer exception. – Manoj Fegde Dec 10 '13 at 06:57
  • but the null pointer was in myContext? or in myContext.getDatabasePath(DATABASE_NAME)? – Carlos Robles Dec 10 '13 at 07:58
  • I updated the code. please run it an tell me where is your error. have you checked your context variable is not null? if its not null, then myContext.getDatabasePath(DATABASE_NAME) returned null, and you have the info on your error in logcat. anyways, this is the way you have to do it for sure. if you are getting nullpointer, is just a related error, dont remove this code and try finding out, im sure you will find it in seconds! – Carlos Robles Dec 10 '13 at 08:10
  • I am getting error as : at android.content.ContextWrapper.getDatabasePtha(ContextWrapper.java:213) – Manoj Fegde Dec 10 '13 at 08:19
  • 1
    I could help with the whole trace. Anyways i cant believe i lost so many reputation (unaprobe unupvote, and an extra downvote) with a answer that is working for much people and meanwhile im online trying to help to find what else could be wrong in the specific case. – Carlos Robles Dec 10 '13 at 09:09
  • if you want to send me the full log by any means, i can help you out. otherwise it will be imposible – Carlos Robles Dec 10 '13 at 09:16
  • also, what is your context? an activity? a service? a null pointer in contextwrapped uses to be that the context that is wrapped is null, wich is really weird. – Carlos Robles Dec 10 '13 at 09:25
  • My contest is an activity. And my context is not null. I am not getting why my app is giving error. And your answer is unupvote by mistake as I just want to remove the acceptance of answer. I will again upvote you. And thanks for your help and need assistance. – Manoj Fegde Dec 10 '13 at 09:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42844/discussion-between-manoj-fegde-and-carlos-robles) – Manoj Fegde Dec 10 '13 at 09:40
  • @ManojFegde: Guessing you're trying to use the activity as a context before `onCreate()` e.g. initializing a member variable or so. You cannot use an activity as a context until `onCreate()` in the activity lifecycle. That's a common cause of NPE in ContextWrapper. – laalto Dec 10 '13 at 10:50
  • yeah, @laalto, we was discussing it at chat, as we thought, the NPE is in the context that the contextwrapper is wrapping. And it happens that was an activity that wasnt started by the system, so on compile time thats ok, but on runtime, that is not a real context, even extending from context. – Carlos Robles Dec 10 '13 at 11:34
  • I think the main problem is `outFile.getPath()` . it should be `getParentFile().getPath()` – mehmet Apr 11 '14 at 06:58
5

Do as follows :

public DataBaseHelperClass(Context context) {       
    super(context, DATABASE_NAME, null ,DATABASE_VERSION);
    this.myContext = context;
    //The Android's default system path of your application database.
    DB_PATH = myContext.getDatabasePath(DATABASE_NAME).getPath();
}

You will not get error or warning.

Optim India
  • 486
  • 1
  • 5
  • 11
2

Just use the context of the activity/app for getting the path like:

Context c = getApplicationContext();
c.getFilesDir().getPath();
insomniac
  • 11,146
  • 6
  • 44
  • 55