如何访问Android项目中的assets文件夹?

3

我可以访问项目文件夹(例如“assets”)中的XML文件,以打开并读取文件中的数据。

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
InputStream iS = this.getAssets().open("myFile.xml");  
Document doc = docBuilder.parse(iS);

但是如果我想编辑文件,我找不到保存它的路径。

TransformerFactory transfac = TransformerFactory.newInstance();
            Transformer trans = transfac.newTransformer();
                //create string from xml tree
                StringWriter sw = new StringWriter();
                StreamResult result = new StreamResult(sw);
                DOMSource source = new DOMSource(doc);
                trans.transform(source, result);
                String xmlString = sw.toString();
                OutputStream f0;
            byte buf[] = xmlString.getBytes();
            **f0 = new FileOutputStream("/assets/wines.xml");**
            //f0 = new FileOutputStream("file:///assets/wines.xml");
            for(int i=0;i<buf .length;i++) {
               f0.write(buf[i]);
            }
            f0.close();
            buf = null;

我总是在这一行遇到FileNotFoundException错误:

f0 = new FileOutputStream("/assets/wines.xml");

有什么想法吗?谢谢!
2个回答

1

如果您正在尝试编辑或写入文件到assets或raw文件夹中,我猜这是不可能的。无法完成


0

最好将其保存到SD卡或内部存储器中

使用File sdDir = [Environment.getExternalStorageDirectory()][1];获取SD卡路径。

别忘了授权许可。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接