Sunday, 27 November 2011

3. How to read the text from a raw text file in an Android app?

Hint: have a look at QuizHelpActivity class, inputStreamToString method.

6 comments:

  1. First create StringBuffer, DataInputStream and a String

    StringBuffer sBuffer = new StringBuffer();
    DataInputStream dataIO = new DataInputStream(is);
    String strLine = null;


    Then read the file and store it in the string


    while ((strLine = dataIO.readLine()) != null) {
    sBuffer.append(strLine + "\n");
    }

    dataIO.close();
    is.close();

    return sBuffer.toString();

    ReplyDelete
  2. InputStream iFile = getResources().openRawResource(R.raw.quizhelp);
    try {
    TextView helpText = (TextView) findViewById(R.id.TextView_HelpText);
    String strFile = inputStreamToString(iFile);
    helpText.setText(strFile);
    } catch (Exception e) {
    Log.e(DEBUG_TAG, "InputStreamToString failure", e);
    }

    ReplyDelete
  3. // Read raw file into string and populate TextView
    InputStream iFile = getResources().openRawResource(R.raw.quizhelp);
    try {
    TextView helpText = (TextView) findViewById(R.id.TextView_HelpText);
    String strFile = inputStreamToString(iFile);
    helpText.setText(strFile);
    } catch (Exception e) {
    Log.e(DEBUG_TAG, "InputStreamToString failure", e);
    }

    ReplyDelete
  4. InputStream iFile = getResources().openRawResource(R.raw.quizhelp);
    try {
    TextView helpText = (TextView) findViewById(R.id.TextView_HelpText);
    String strFile = inputStreamToString(iFile);
    helpText.setText(strFile);
    }

    Input stream to string is explained how to read the data
    public String inputStreamToString(InputStream is) throws IOException {
    StringBuffer sBuffer = new StringBuffer();
    DataInputStream dataIO = new DataInputStream(is);
    String strLine = null;

    while ((strLine = dataIO.readLine()) != null) {
    sBuffer.append(strLine + "\n");
    }

    dataIO.close();
    is.close();

    return sBuffer.toString();
    }

    ReplyDelete
  5. Retrieve an input stream of the required raw file
    Create an instance of DataInputStream which reads the previous stream as input.
    Read line by line until we reach the end of file and append each line to a stringbuffer
    Close the dataInputstream
    InputStream iFile = getResources().openRawResource(R.raw.quizhelp);
    try {
    TextView helpText = (TextView) findViewById(R.id.TextView_HelpText);
    String strFile = inputStreamToString(iFile);
    helpText.setText(strFile);
    } catch (Exception e) {
    Log.e(DEBUG_TAG, "InputStreamToString failure", e);
    }

    public String inputStreamToString(InputStream is) throws IOException {
    StringBuffer sBuffer = new StringBuffer();
    DataInputStream dataIO = new DataInputStream(is);
    String strLine = null;

    while ((strLine = dataIO.readLine()) != null) {
    sBuffer.append(strLine + "\n");
    }

    dataIO.close();
    is.close();

    return sBuffer.toString();
    }

    ReplyDelete
  6. Input Stream iFile = getResourses ().openRawResource(R.raw.quizhelp);
    try {
    TextView helpText = (TextView) findViewById(R.id.TextView_HelpText);
    String strFile = inputStreamToString(iFile);
    helpText.setText(strFile);
    } catch (Exception e) {
    Log.e(DEBUG_TAG, "InputStreamToString failure", e);
    }
    public String inputStreamToString(InputStream is) throws IOException {
    StringBuffer sBuffer = new StringBuffer();
    DataInputStram dataIO = new DataInputStream(is);
    String strLine = null;
    while ((strLine = dataIO.readLine()) != null) {
    sBuffer.append(strLine + "\n");
    }
    dataIO.close();
    is.close();
    return sBuffer.toString();
    }

    ReplyDelete