3

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 } } 
2
  • 1
    some java code would be nice man... Commented Mar 23, 2013 at 11:15
  • @Sajmon I added the line Commented Mar 23, 2013 at 11:23

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

Comments

5

How about:

InputStream is = getActivity().getAssets().open("file1.txt"); 

1 Comment

Again an error comes saying "The method getActivity() is undefined for the type ClassName.".
4

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.