0

I have a text file that I am pulling from my SD card which contains an array in plain text format. Here are the contents of that file ...

http://www.oddree.com/rayhaque/android1.jpg,http://www.oddree.com/rayhaque/android2.jpg,http://www.oddree.com/rayhaque/android3.jpg,http://www.oddree.com/rayhaque/android4.jpg,http://www.oddree.com/rayhaque/android5.jpg

I am trying to import that text file into a String, convert that String to an array, and then load that array into a list adapter. If I try to split up readString or a trimmed result of readString, I get a forced close every time. If I copy the contents of the file into a String and then use that ... everything works as expected.

So what is the difference between loading this stuff from a text file, and loading it from an included string? Is it a byte conversion issue?

Here is my code. I have noted what works and what fails.

public class MainActivity extends Activity {

ListView list;
LazyAdapter adapter;
List<String> strings = new ArrayList<String>();
String readString = new String();
String arrayNBOW;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    try{
     File f = new File(Environment.getExternalStorageDirectory()+"/LazyList/gkindex.txt");
     FileInputStream fileIS = new FileInputStream(f);
     BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));

     while((readString = buf.readLine())!= null){
      String arrayNBOW = readString.trim();
      Toast.makeText(MainActivity.this, "STARTUPPULL: "+arrayNBOW, Toast.LENGTH_LONG).show();
     }
  } catch (FileNotFoundException e) {
   Toast.makeText(MainActivity.this, "FAIL: "+e, Toast.LENGTH_LONG).show();
   e.printStackTrace();
  } catch (IOException e){
   Toast.makeText(MainActivity.this, "FAIL: "+e, Toast.LENGTH_LONG).show();
   e.printStackTrace();
  }

    setContentView(R.layout.main);
    list=(ListView)findViewById(R.id.list);

    String testArray = "http://www.oddree.com/rayhaque/android1.jpg,http://www.oddree.com/rayhaque/android2.jpg,http://www.oddree.com/rayhaque/android3.jpg";

    // THIS FAILS
    // String[] testArraySplit = TextUtils.split(arrayNBOW, ",");

    // THIS WORKS
    String[] testArraySplit = TextUtils.split(testArray, ",");

    adapter=new LazyAdapter(this, testArraySplit);
 list.setAdapter(adapter);

    Button b=(Button)findViewById(R.id.button1);
    b.setOnClickListener(listener);

    Button c=(Button)findViewById(R.id.button2);
    c.setOnClickListener(loadtext);

}

Thank you in advance for any advice or assistance you can offer me! :-)

SOLUTION CREATED FROM EMANNUEL'S SUGGESTION:

public class MainActivity extends Activity {

ListView list;
LazyAdapter adapter;
List<String> strings = new ArrayList<String>();
String readString = new String();
String arrayNBOW;
String[] nardsArray;
String nards;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

// New Solution

  File sdcard = Environment.getExternalStorageDirectory();
  File file = new File(sdcard,"/LazyList/gkindex.txt");
  StringBuilder text = new StringBuilder();

  try {
      BufferedReader br = new BufferedReader(new FileReader(file));
      String line;

      while ((line = br.readLine()) != null) {
          text.append(line);
          text.append('\n');
          nards = text.toString();
          nardsArray = TextUtils.split(nards, ",");
      }
  }
  catch (IOException e) {
      Toast.makeText(MainActivity.this, "FAIL: "+e, Toast.LENGTH_LONG).show();
  }

// End New Solution

    Toast.makeText(MainActivity.this, "FILE-READ: "+nardsArray, Toast.LENGTH_LONG).show();

    setContentView(R.layout.main);
    list=(ListView)findViewById(R.id.list);

    adapter=new LazyAdapter(this, nardsArray);
    list.setAdapter(adapter);

    Button b=(Button)findViewById(R.id.button1);
    b.setOnClickListener(listener);

    Button c=(Button)findViewById(R.id.button2);
    c.setOnClickListener(loadtext);

}
RayHaque
  • 171
  • 1
  • 4
  • 9

1 Answers1

2

I suspect your reading didn't work. First thing I noticed is that there's no finally clause for closing streams. Second is you're looping until the end of the file. What if you had an empty line there? Check for empty "" lines. Also you should break on the first line that has contents.

Also, I prefer to use a FileReader like this:

Does this help?

Community
  • 1
  • 1
Emmanuel
  • 16,791
  • 6
  • 48
  • 74
  • Yeah if the reading failed, then arrayNBOW would still be null when you call split leading to a NullPointerException and a force close. – Josh Clemm Nov 10 '10 at 17:28
  • Thank you Emmanuel. That example works far better. Not knowing how to convert from a StringBuilder to an Array, I am converting first to a String, and then to an Array. There may be a better way to handle this, but it works for my purposes! I am posting my new code in the Question contents. – RayHaque Nov 10 '10 at 18:57