Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve method openFileInput (Android Development)

Tags:

java

android

I'm getting a 'cannot resolve method' error with the line of code:

FileInputStream fis = openFileInput(fileName);

I'm not in an acitivty I'm in a seperate class so I'm assuming that's why it won't work. I've tried doing things like this but they still give me the 'cannot resolve method error:

FileInputStream fis = getApplicationContext.openFileInput(fileName);

Thanks for any help, I'm new to android development

like image 972
Derek Schuster Avatar asked Nov 30 '14 22:11

Derek Schuster


2 Answers

I had a similar case of this, where I couldn't use openFileInput or openFileOutput while in a class that wasn't an activity. In my case, though, I was already passing the context to the method, so I just did

context.openFileInput("stuff.dat");

If you're able to do something similar, that solves it.

like image 101
Mike Avatar answered Oct 08 '22 13:10

Mike


Main error is due to Context please give the right context or you can pass context by using it in function. for this you will need to pass Activity Context to it by sending Context using parametrized method as:

  protected void onCreate(String filename,Context context) {
  try
 {
     FileInputStream fis = context.openFileInput(filename);  
     //...your code here...      
 }
 catch (Exception ex)
 {

 }
}
like image 44
Muhammad Kashif Arif Avatar answered Oct 08 '22 12:10

Muhammad Kashif Arif