0

I'm trying to parse a local .xml file using XmlPullParseFactory. I'm having trouble accessing the path "/storage/emulated/0/Download/testcards.xml" despite having applied permission in the manifest file.

public void parseXML(){
    XmlPullParserFactory parserFactory;
    try{
        parserFactory = XmlPullParserFactory.newInstance();
        XmlPullParser parser = parserFactory.newPullParser();

        InputStream is = new FileInputStream(new File("/storage/emulated/0/Download/testcards.xml"));

        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(is,null);

        processParsing(parser);
    } catch(XmlPullParserException e){

    } catch (IOException e) {

    }

}

My manifest file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="demoapp1domain.demoapp1">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
.....

When I try to run my program I get:

java.io.FileNotFoundException: /storage/emulated/0/Download/testcards.xml (Permission denied)

This is the code for getting the path:

public void startFileExplorer(View view) {

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("text/xml");   
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    try {
        startActivityForResult(Intent.createChooser(intent, "Select your flashcards .XML-file "), REQUEST_CHOOSER);
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_CHOOSER:
            if (resultCode == RESULT_OK) {
                final Uri uri = data.getData();
                String myXMLFilePath = uri.getPath();
            }
            break;
    }
}

Any idea why permission is denied?

android {
compileSdkVersion 26
defaultConfig {
    applicationId "demoapp1domain.demoapp1"
    minSdkVersion 21
    targetSdkVersion 26
Erik
  • 21
  • 1
  • 2
  • 2
    https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it – CommonsWare Feb 23 '18 at 12:41
  • Also would be advised to use the Environment.DIRECTORY_DOWNLOADS instead of hardcoding the path. Your problem is you need to ask for the read permission at runtime because it is a dangerous permission – Alex Feb 23 '18 at 12:48

1 Answers1

1

Thanks CommonsWare and Alex, obviously you were right! It works now that I ask for permission in runtime.

 String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE};
        requestPermissions(permissions,PERMISSION_REQUEST_CODE);
        InputStream is = new FileInputStream(new File("/storage/emulated/0/Download/testcards.xml"));
Erik
  • 21
  • 1
  • 2