android 怎么保存文件怎么打开方式,打开并保存文件路径android

我发现我自己的解决方案,比我认为的更容易。但还是谢谢你@Pedram

在的onCreate:

Uri data = getIntent().getData();

if(data!=null)

{

getIntent().setData(null);

try {

importData(data);

} catch (Exception e) {

// warn user about bad data here

Log.d("Docspro", "Opening file failed");

e.printStackTrace();

}

}

然后做在同一个类中的方法:

private void importData(Uri data) {

final String scheme = data.getScheme();

if(ContentResolver.SCHEME_CONTENT.equals(scheme)) {

try {

ContentResolver cr = this.getContentResolver();

InputStream is = cr.openInputStream(data);

if(is == null) return;

StringBuffer buf = new StringBuffer();

BufferedReader reader = new BufferedReader(new InputStreamReader(is));

String str;

if (is!=null) {

while ((str = reader.readLine()) != null) {

buf.append(str + "\n");

}

}

is.close();

// perform your data import here…

ParseXML(buf.toString());

}

catch(IOException e){

}

}

}

然后最终方法解析器:

public void ParseXML(String file)

{

try {

InputStream is = new ByteArrayInputStream(file.getBytes("UTF-8"));

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

Document doc = dBuilder.parse(is);

//optional, but recommended

//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work

doc.getDocumentElement().normalize();

Log.i("Testing", "Root element :" + doc.getDocumentElement().getNodeName());

NodeList nList = doc.getElementsByTagName("Settings");

Log.i("Testing","----------------------------");

for (int temp = 0; temp < nList.getLength(); temp++) {

Node nNode = nList.item(temp);

Log.d("Testing", "\nCurrent Element :" + nNode.getNodeName());

if (nNode.getNodeType() == Node.ELEMENT_NODE) {

Element eElement = (Element) nNode;

DCSEmail = eElement.getElementsByTagName("User").item(0).getTextContent();

if(DCSEmail.equals(Settings.getEmail()))

{

Settings.setAccount(true);

Log.d("waarde account", "Waarde : " + Settings.getAccount() + " & Waarde DCSEMAIL : " + DCSEmail);

}

else

{

Settings.setAccount(false);

}

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

我调整了方法到我自己的xml文件,我需要读出。希望这将有助于未来的其他人!

注:的IMPORTDATA给文件作为内容的示例:

XML

配置

设置

等(带支架<>图片它们).. 。

所以要解决这个问题,我只是用bytearrayinputstream输入流。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值