I have a text file stored in sdcard.I want to copy the data from text file in sdcard into a string array.I want to store data in string array and then later use that data in my autocompletetextview.My code is:
package com.example.pro2;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ReadSettings();
}
});
}
public void ReadSettings(){
try{
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"comm.txt");
FileInputStream fileIS = new FileInputStream(file);
BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
String readString = new String();
String val[] = null;
for(int i=0;i<5;i++)
{
while ((readString = buf.readLine()) != null) {
Log.d("line: ", readString);
val[i] = readString;
Log.d("values", val[i]);
// u better use an ArrayList or you have to check if i < val.size
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
}
But it is showing force close.Plz help.
Not possibly an answer, please change here: