[英]What's the way to access the assets-folder in an Android project?
I access to an XML file placed in a project folder (for instance 'assets') for open and read data from file. 我访问放置在项目文件夹(例如“资产”)中的XML文件,以打开文件并从文件中读取数据。
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
InputStream iS = this.getAssets().open("myFile.xml");
Document doc = docBuilder.parse(iS);
but If I want to edit the file, I can't find the path to save it. 但是如果要编辑该文件,则找不到保存该文件的路径。
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;
I obtain always a FileNotFoundException
at line: 我总是在以下行获得
FileNotFoundException
:
f0 = new FileOutputStream("/assets/wines.xml");
Any ideas? 有任何想法吗? Thanks!
谢谢!
If you are trying to edit or write
file to assets or raw folder
. 如果您要
edit or write
文件edit or write
文件edit or write
assets or raw folder
。 I guess it is not possible. 我想这是不可能的。 It cannot be done
无法做到
Bettery try saving it to sd card or internal storage 最好尝试将其保存到SD卡或内部存储中
Use File sdDir = [Environment.getExternalStorageDirectory()][1];
使用
File sdDir = [Environment.getExternalStorageDirectory()][1];
to get the sdcard path. 获取sdcard路径。
And don't forget to give permission 并且不要忘记给予许可
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.