-1

我想在android中读取一个文本文件。我正在尝试这段代码。

public class GIF_Activity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gif_);
        try {
            PlayWithRawFiles();
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(), 
                         "Problems: " + e.getMessage(), 1).show();
        }
    }

    public void PlayWithRawFiles() throws IOException {      
        String str="";
        StringBuffer buf = new StringBuffer();          
        InputStream is = this.getResources().openRawResource(R.drawable.abc);
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        if (is!=null) {                         
            while ((str = reader.readLine()) != null) { 
                buf.append(str + "\n" );
            }               
        }       
        is.close(); 
        Toast.makeText(getBaseContext(), 
                buf.toString(), Toast.LENGTH_LONG).show();              


    }

    }

但不是让文件显示在屏幕上,而是在烤文件。请帮我阅读完整的文件。提前致谢。

4

2 回答 2

1

该文件正在烘烤,因为您正在烘烤该文件。

Toast.makeText(getBaseContext(), 
                buf.toString(), Toast.LENGTH_LONG).show();

使用 TextView 或 TextArea 来显示文件内容,而不是使用 toast。

TextView tv = (TextView) findViewById(<id of the textview>);
tv.setText(buf.toString());
于 2013-10-23T10:41:52.117 回答
0

这一行:

Toast.makeText(getBaseContext(), 
                buf.toString(), Toast.LENGTH_LONG).show();    

是您将文件吐司到屏幕上的位置。

你想把它放在哪里?你能展示一下activity_gif_.xml吗?

于 2013-10-23T10:41:38.497 回答