0
votes

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)

1

1 Answers

0
votes

Actually problem in file path .your file path is not vaild so find real path of file

 String filePath = getRealPathFromURI(uri);

getRealPathFromURI methods

private String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(mContext, contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String result = cursor.getString(column_index);
cursor.close();
return result;
 }