In my app the user can select a xml file via an Intent:
Selecting:
Intent chooseFileXML = new Intent(Intent.ACTION_GET_CONTENT);
chooseFileXML.setType("text/xml");
Intent intentXML = Intent.createChooser(chooseFileXML, getString(R.string.importXMLDatei));
startActivityForResult(intentXML, REQUEST_CODE_IMPORT_XML_FILE);
Receiving:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
switch (requestCode){
case REQUEST_CODE_IMPORT_XML_FILE:
if(resultCode == RESULT_OK){
Uri uri = data.getData();
String filePath = uri.getPath();
File fl = new File(filePath);
//Get xml-code from file and put it in a String
FileInputStream fin = null;
try {
fin = new FileInputStream(fl);
BufferedReader reader = new BufferedReader(new InputStreamReader(fin));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
System.out.println(sb.toString());
fin.close();
} catch (Exception e) {
e.printStackTrace();
}
}
break;
}
}
I receive the correct filepath. But in this line: fin = new FileInputStream(fl); I get this error:
java.io.FileNotFoundException: /document/primary:Android/data/com.oli.myapp/Files/test.xml: open failed: ENOENT (No such file or directory)