DroidForums.net is the original Verizon Android Forum! Registered Users do not see these ads. Please Register - It's Free!
Results 1 to 2 of 2

Thread: How to make Android Read txt file and setText or append to a TextView

  1. Junior Droid
    r2sjoblom's Avatar
    Member #
    173795
    Join Date
    Feb 2011
    Posts
    10
    Phone
    droid x
    #1

    How to make Android Read txt file and setText or append to a TextView

    Below is a sample from one of my class files that I am having problems with. What I really just want to do is to add a button to open a new Intent Activity with ACTION_VIEW to display the text file. This wasn't working inside the OnClickListener:

    File file = new File(file://android_assets/browsercode.txt);Intent newintent = new Intent(android.conteent.Intent.ACTION_VIEW);
    newintent.setData(file);
    startActivity(newintent);

    It just gave me an error. So instead i am going to just try to read the file's contents into a string and output it to a TextView. This wasn't working either. If anyone has any suggestion on either way, that would be greatful. I have tried many variations of each and got NOWHERE. I perfer the one above with the button click though.

    Thanks in advance for any help.

    public
    class CodeSlider extends Activity {
    TextView
    textcode;
    @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.
    codeslider);
    textcode
    = (TextView)findViewById(R.id.codetext);
    readthefile();
    }
    privatevoid readthefile() {
    //
    TODO Auto-generated method stub
    File file =
    new File(file://android_assets/browsercode.txt);
    int i=0;
    FileReader into =
    null;
    try
    {into = new FileReader(file); // And a char stream to read it
    char[] buffer = newchar[4096]; // Read 4K characters at a time
    int len; // How many chars read each time
    while ((len = into.read(buffer)) != -1){
    String s =
    new String(buffer, 0, len);
    i++;

    textcode
    .append(s);
    }
    }
    catch(IOException e) {
    System.
    out.println("Error");return;
    }
    }
    }
    Last edited by r2sjoblom; 12-05-2011 at 06:22 PM.
  2.  
     
     
     
  3. Junior Droid
    r2sjoblom's Avatar
    Member #
    173795
    Join Date
    Feb 2011
    Posts
    10
    Phone
    droid x
    #2

    Got it!

    I needed to use AssetManager API to import the stuff in the assets/ directory. Below is the code for anyone that needs it.

    import

    java.io.IOException;

    import

    java.io.InputStream;

    import

    android.app.Activity;

    import

    android.content.res.AssetManager;

    import

    android.os.Bundle;

    import

    android.widget.TextView;

    public

    class CodeSlider extends Activity {
    @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.
    codeslider);
    TextView text = (TextView)findViewById(R.id.
    codetext);
    AssetManager am = CodeSlider.
    this.getAssets();
    int i=0;try {InputStream is = am.open("browsercode.txt");
    byte
    [] buffer = newbyte[4096]; // Read 4K characters at a time
    int len; // How many chars read each time
    while ((len = is.read(buffer)) != -1){
    String s =
    new String(buffer, 0, len);
    text.append( s );
    i++;

    }
    }
    catch(IOException e) {
    System.
    out.println("Error");
    return
    ;
    }
    }

    }

Links

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Similar Threads

  1. Deleted contacts, read on as hidden file?
    By R2A5 in forum Android Tech Support
    Replies: 1
    Last Post: 11-10-2011, 12:35 PM
  2. Replies: 2
    Last Post: 01-25-2011, 08:37 AM
  3. SBF file caused infinite loop, read everywhere, need help
    By 66racer in forum Droid X Rooted Help
    Replies: 9
    Last Post: 09-02-2010, 09:24 AM
  4. Possible to make droid read like a ipod?
    By djkms in forum Android General Discussions
    Replies: 1
    Last Post: 06-24-2010, 10:54 AM
  5. Append contact info?
    By Solcutter in forum Android General Discussions
    Replies: 5
    Last Post: 03-13-2010, 07:33 PM

Search tags for this page

android app read text file
,
android open text file example
,

how to make android read

,
how to make the android read
,
how to open txt file in android
,
open text file android
,
open txt files on android
,
open txt on android
,
opening txt file on android
,
using uri to read text file android
Click on a term to search our site for related topics.

Tags for this Thread

Find us on Google+