我正在尝试使用XmlPullParseFactory解析本地.xml文件。 尽管在清单文件中应用了权限,但是我无法访问路径“ /storage/emulated/0/Download/testcards.xml” 。

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) {

}

}

我的清单文件:

package="demoapp1domain.demoapp1">

.....

当我尝试运行程序时,我得到:

java.io.FileNotFoundException:/storage/emulated/0/Download/testcards.xml (权限被拒绝)

这是获取路径的代码:

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;

}

}

知道为什么拒绝权限吗?

android {

compileSdkVersion 26

defaultConfig {

applicationId "demoapp1domain.demoapp1"

minSdkVersion 21

targetSdkVersion 26

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐