Skip to content Skip to sidebar Skip to footer

Android File I/O OpenFileInput() Undefined

package com.example.tictactoeshowgrid; import android.os.Bundle; import java.io.*; import android.widget.Toast; import

Solution 1:

for accessing openFileInput method in non Activity class you will need to pass Activity Context to it by sending Context using parametrized method or using ImportOBJ class constructor as:

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

     }
}

and from your Activity pass context as:

ImportOBJ obj_import=new ImportOBJ();
obj_import.onCreate(<File_Name_Here>,Your_Current_Activity_Name.this);

Solution 2:

If error is :

The method openFileInput(String) is undefined for the type ...

You have to use openFileInput in a Context.For example an Activity is a Context.So if you cahnge your class to this,error must be solve:

public class ImportOBJ extends Activity{
.
.
.

}

You can see What is Context in Android? for more details on Context.


Post a Comment for "Android File I/O OpenFileInput() Undefined"