开发者

What's the way to access the assets-folder in an Android project?

开发者 https://www.devze.com 2023-04-09 01:09 出处:网络
I access to an XML file placed in a project folder (for instance \'assets\') for open and read data from file.

I access to an XML file placed in a project folder (for instance 'assets') for open and read data from file.

DocumentBuild开发者_如何学JAVAerFactory 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:

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

Any ideas? Thanks!


If you are trying to edit or write file to assets or raw folder. I guess it is not possible. It cannot be done


Bettery try saving it to sd card or internal storage

Use File sdDir = [Environment.getExternalStorageDirectory()][1]; to get the sdcard path.

And don't forget to give permission

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号