How to read a selected text file from sdcard on android

后端 未结 3 506
旧巷少年郎
旧巷少年郎 2020-12-06 14:37

i am new at android development and i need your help. I was locking at topics that are similar for my development but non of then help me. So far i create functions that get

3条回答
  •  有刺的猬
    2020-12-06 15:03

    Try this code:

    package com.javasamples;
    import java.io.*;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.*;
    import android.view.View.OnClickListener;
    import android.widget.*;
    
    public class FileDemo2 extends Activity {
        // GUI controls
        EditText txtData;
        Button btnWriteSDFile;
        Button btnReadSDFile;
        Button btnClearScreen;
        Button btnClose;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // bind GUI elements with local controls
        txtData = (EditText) findViewById(R.id.txtData);
        txtData.setHint("Enter some lines of data here...");
    
        btnWriteSDFile = (Button) findViewById(R.id.btnWriteSDFile);
        btnWriteSDFile.setOnClickListener(new OnClickListener() {
    
        public void onClick(View v) {
            // write on SD card file data in the text box
            try {
                File myFile = new File("/sdcard/mysdfile.txt");
                myFile.createNewFile();
                FileOutputStream fOut = new FileOutputStream(myFile);
                OutputStreamWriter myOutWriter = 
                                        new OutputStreamWriter(fOut);
                myOutWriter.append(txtData.getText());
                myOutWriter.close();
                fOut.close();
                Toast.makeText(getBaseContext(),
                        "Done writing SD 'mysdfile.txt'",
                        Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                Toast.makeText(getBaseContext(), e.getMessage(),
                        Toast.LENGTH_SHORT).show();
            }
        }// onClick
        }); // btnWriteSDFile
    
            btnReadSDFile = (Button) findViewById(R.id.btnReadSDFile);
            btnReadSDFile.setOnClickListener(new OnClickListener() {
    
            public void onClick(View v) {
                // write on SD card file data in the text box
            try {
                File myFile = new File("/sdcard/mysdfile.txt");
                FileInputStream fIn = new FileInputStream(myFile);
                BufferedReader myReader = new BufferedReader(
                        new InputStreamReader(fIn));
                String aDataRow = "";
                String aBuffer = "";
                while ((aDataRow = myReader.readLine()) != null) {
                    aBuffer += aDataRow + "\n";
                }
                txtData.setText(aBuffer);
                myReader.close();
                Toast.makeText(getBaseContext(),
                        "Done reading SD 'mysdfile.txt'",
                        Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                Toast.makeText(getBaseContext(), e.getMessage(),
                        Toast.LENGTH_SHORT).show();
            }
            }// onClick
            }); // btnReadSDFile
    
            btnClearScreen = (Button) findViewById(R.id.btnClearScreen);
            btnClearScreen.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
                    // clear text box
                    txtData.setText("");
                }
            }); // btnClearScreen
    
            btnClose = (Button) findViewById(R.id.btnClose);
            btnClose.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
                    // clear text box
                    finish();
                }
            }); // btnClose
    
        }// onCreate
    
    }// AndSDcard
    

    the layout file is

    
    
    
    
    

    enter image description here

提交回复
热议问题