Android文件I / O openFileInput()未定义 [英] Android File I/O openFileInput() undefined

查看:359
本文介绍了Android文件I / O openFileInput()未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

            package com.example.tictactoeshowgrid;

            import android.os.Bundle;
            import java.io.*;
            import android.widget.Toast;
            import android.content.*;

            import java.io.FileInputStream;
            import java.io.FileNotFoundException;
            import java.io.FileOutputStream;
            import java.io.IOException;
            import java.util.Date;

            import android.content.Context;

                public class ImportOBJ {

                protected void onCreate(String filename) {
                    try
                    {
                        FileInputStream fis = openFileInput(filename);
                        BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
                        String line = null, input="";
                        while ((line = reader.readLine()) != null)
                            input += line;
                        reader.close();
                        fis.close();
                        //toast("File successfully loaded.");
                        //return input;
                    }
                    catch (Exception ex)
                    {
                        //toast("Error loading file: " + ex.getLocalizedMessage());
                        //return "";
                    }
                }
            }

我在看文件I / O为Android的一个例子。我很奇怪,为什么openFileInput快到了一个错误作为一个未定义的功能。我在想,也许我错过了一个进口?如果没有,那么就必须有一些其他的问题。

I am looking at an example of File I/O for Android. I am wondering why openFileInput is coming up with an error as being an undefined function. I was thinking that maybe I missed an import? If not then there must be some other problem.

在此先感谢...

推荐答案

在非活性类访问 openFileInput 方法,你将需要传递活动上下文给它发送上下文采用参数化方法或使用 ImportOBJ 类的构造函数为:

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)
     {

     }
}

和从活动传球方面为:

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

这篇关于Android文件I / O openFileInput()未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆