LefmyHans
Lurker
Good evening I'm facing a problem in my code. I would like to parse a set of kml file to extract the contents and put it in bae of data. it returns each time the list of files, but not the contents of each file. Please help me
Here my MainActivity.java file content :
public class MainActivity extends AppCompatActivity {
private TextView txt;
private TextView texte;
ListView listView;
private List<String> fileNameList;
private FlAdapter mAdapter;
private File file;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED){
//FetchImages();
listView = (ListView) findViewById(R.id.listView);
file = Environment.getExternalStorageDirectory();
fileNameList = getFileListfromSDCard();
mAdapter = new FlAdapter(this, R.layout.mylist, fileNameList);
listView.setAdapter(mAdapter);
}
else{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}
else{
//startActivity(new Intent(getActivity(), SQLite2ExcelActivity.class));
}
}
private List<String> getFileListfromSDCard() {
String state = Environment.getExternalStorageState();
ArrayList<String> filenames = new ArrayList<String>();
String path = Environment.getExternalStorageDirectory()
+ File.separator + "Fodecc_csv";
File directory = new File(path);
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++)
{
if(files.isFile() && files.getPath().endsWith(".kml")){
String file_name = files.getName();
// you can store name to arraylist and use it later
filenames.add(file_name);
XmlPullParserFactory parserFactory;
try {
parserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = parserFactory.newPullParser();
//File file = new File("/storage/emulated/0/Kml.kml");
InputStream is = new FileInputStream(file_name);
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(is, null);
processParsing(parser);
//InputStream is = getAssets().open("kml.xml");
} catch (XmlPullParserException e) {
} catch (IOException e) {
}
}
}
return filenames;
}
private void processParsing(XmlPullParser parser) throws IOException, XmlPullParserException{
ArrayList<coordonnees> coordo = new ArrayList<>();
int eventType = parser.getEventType();
coordonnees currentcoordo= null;
while (eventType != XmlPullParser.END_DOCUMENT) {
String eltName = null;
switch (eventType) {
case XmlPullParser.START_TAG:
eltName = parser.getName();
if ("Placemark".equals(eltName)) {
currentcoordo = new coordonnees();
coordo.add(currentcoordo);
} else if (currentcoordo != null) {
if ("name".equals(eltName)) {
currentcoordo.name = parser.nextText();
}
}
break;
}
eventType = parser.next();
}
printcoordo(coordo);
}
private void printcoordo(ArrayList<coordonnees> coordo) {
StringBuilder builder = new StringBuilder();
for (coordonnees Placemark : coordo) {
builder.append(Placemark.name).append("\n");
}
txt = (TextView) findViewById(R.id.te);
txt.setText(builder.toString());
}
}
Here my MainActivity.java file content :
public class MainActivity extends AppCompatActivity {
private TextView txt;
private TextView texte;
ListView listView;
private List<String> fileNameList;
private FlAdapter mAdapter;
private File file;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED){
//FetchImages();
listView = (ListView) findViewById(R.id.listView);
file = Environment.getExternalStorageDirectory();
fileNameList = getFileListfromSDCard();
mAdapter = new FlAdapter(this, R.layout.mylist, fileNameList);
listView.setAdapter(mAdapter);
}
else{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}
else{
//startActivity(new Intent(getActivity(), SQLite2ExcelActivity.class));
}
}
private List<String> getFileListfromSDCard() {
String state = Environment.getExternalStorageState();
ArrayList<String> filenames = new ArrayList<String>();
String path = Environment.getExternalStorageDirectory()
+ File.separator + "Fodecc_csv";
File directory = new File(path);
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++)
{
if(files.isFile() && files.getPath().endsWith(".kml")){
String file_name = files.getName();
// you can store name to arraylist and use it later
filenames.add(file_name);
XmlPullParserFactory parserFactory;
try {
parserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = parserFactory.newPullParser();
//File file = new File("/storage/emulated/0/Kml.kml");
InputStream is = new FileInputStream(file_name);
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(is, null);
processParsing(parser);
//InputStream is = getAssets().open("kml.xml");
} catch (XmlPullParserException e) {
} catch (IOException e) {
}
}
}
return filenames;
}
private void processParsing(XmlPullParser parser) throws IOException, XmlPullParserException{
ArrayList<coordonnees> coordo = new ArrayList<>();
int eventType = parser.getEventType();
coordonnees currentcoordo= null;
while (eventType != XmlPullParser.END_DOCUMENT) {
String eltName = null;
switch (eventType) {
case XmlPullParser.START_TAG:
eltName = parser.getName();
if ("Placemark".equals(eltName)) {
currentcoordo = new coordonnees();
coordo.add(currentcoordo);
} else if (currentcoordo != null) {
if ("name".equals(eltName)) {
currentcoordo.name = parser.nextText();
}
}
break;
}
eventType = parser.next();
}
printcoordo(coordo);
}
private void printcoordo(ArrayList<coordonnees> coordo) {
StringBuilder builder = new StringBuilder();
for (coordonnees Placemark : coordo) {
builder.append(Placemark.name).append("\n");
}
txt = (TextView) findViewById(R.id.te);
txt.setText(builder.toString());
}
}