Reputation: 31
I am trying to open a file which is in the assets folder. But using getAssets() gives the error given above. I know I have to pass the context from another activity, but I can't do that either as then another error comes-"The method onCreate(SQLiteDatabase, Context) of type ClassName must override or implement a supertype method". So I am stuck. Is there a better way of opening that file? Here is the line:
InputStream is = getAssets().open("file1.txt");
*Note: ClassName is not an activity, it's just a class, so getAssets() cannot work without passing context from another activity.
Edit: Here is the class and onCreate declaration:
public class DatabaseHandler extends SQLiteOpenHelper {
@Override
public void onCreate(SQLiteDatabase db) {//some stuff
InputStream is = getAssets().open("file1.txt");
//more stuff
}
}
Upvotes: 3
Views: 15860
Reputation: 75778
Use getContext()
Method. Set getContext().getAssets()
. I hope it will works .Actually getAssets() is a method on Context .
For more details you can visit Here
Upvotes: 4
Reputation: 1006549
I am trying to open a file which is in the assets folder. But using getAssets() gives the error given above.
getAssets()
is a method on Context
.
I know I have to pass the context from another activity, but I can't do that either as then another error comes-"The method onCreate(SQLiteDatabase, Context) of type ClassName must override or implement a supertype method".
Since you declined to paste the source code where this is occurring, it is difficult to help you.
ClassName is not an activity, it's just a class
More specifically, it is a subclass of SQLiteOpenHelper
.
so getAssets() cannot work without passing context from another activity.
A SQLiteOpenHelper
gets passed a Context
to its constructor, which you need to override.
Beyond all of this, if your objective is to package a database with your app, please use SQLiteAssetHelper
, as it has solved this problem.
Upvotes: 6
Reputation: 14667
How about:
InputStream is = getActivity().getAssets().open("file1.txt");
Upvotes: 5