[android-developers] Re: Saving form data to XML
Hi, Use File.separator instead of /. This might be obvious but make sure SD card is not mounted on PC when running your code & debugging. Yes Context Provider is 'best' particularly for sharing with other apps, but involves a lot more work. Also I would not bother to check if file exits & create it I would just write to it regardless unless you don't want to overwrite it. Regards On Sep 26, 10:35 pm, ksmobilejava wrote: > To create new xml file on SD card I am using following code > String strFileName = Environment.getExternalStorageDirectory()+"/" + > "temp.xml"; > try{ > > if(!lXmlfile.exists()) > { > > lXmlfile.createNewFile(); > } > > }catch(IOException e){ > > > Log.e("IOException", e.toString()); > > return null; > } > Above code giving me IO exception Permission denied. Although I have > added permission in AndroidManifest.xml > like: > android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> > > still , can not create file. where am I going wrong? how can I test > this on emulator (create file on which path?) .Pls help its urgent. > One more question: Is user defined content provider is the best way to > complete this requirement(saving and retrieving form data that also > contains image). Pls share. > Regards > > On Sep 26, 7:41 am, gjs wrote: > > > > > > > > > Hi, > > > Seehttp://developer.android.com/reference/android/content/Context.html#g...() > > > andhttp://developer.android.com/guide/topics/data/data-storage.html > > > Regards > > > On Sep 23, 7:28 pm, ksmobilejava wrote: > > > > Hello , > > > developing application withformhaving multiple fields, at the end > > > user need to submit the information. > > > On this event, all thedataneed to be stored inXMLfile. > > > thusXMLwrite is required. plus when user asks for particular id then > > > thedatafilled by that id is also to be displayed thusXMLread is > > > required.(search operation) > > > Following is the code I tried for generating samplexml. > > > /// > > > // > > > FileOutputStream fileos = null; > > > try{ > > > > > > fileos=openFileOutput("newxml.xml",Context.MODE_PRIVATE); > > > Log.v("info","fileoutput open"); > > > // fileos = new > > > FileOutputStream(newxmlfile); > > > }catch(FileNotFoundException e){ > > > Log.e("FileNotFoundException", > > > "can't create > > > FileOutputStream"); > > > } > > > // create a XmlSerializer in order to > > > writexmldata > > > XmlSerializer serializer > > > =Xml.newSerializer(); > > > try { > > > //we set the FileOutputStream as > > > output for the > > > serializer, using UTF-8 encoding > > > > > > serializer.setOutput(fileos, "UTF-8"); > > > //Write > > with encoding (if encoding not null) > > > and standalone flag (if standalone not null) > > > > > > serializer.startDocument(null, Boolean.valueOf(true)); > > > //set indentation option > > > > > > //serializer.setFeature("http://xmlpull.org/v1/doc/ > > > features.html#indent-output", true); > > > //start a tag called > > > "root" > > > serializer.startTag(null, > > > "root"); > > > //i indent code just to > > > have a view similar toxml-tree > > > > > > serializer.startTag(null, "child1"); > > > > > > serializer.endTag(null, "child1"); > > > > > > > serializer.startTag(null, "child2"); > > > //set an > > > attribute called "attribute" with a "value" for
[android-developers] Re: Saving form data to XML
To create new xml file on SD card I am using following code String strFileName = Environment.getExternalStorageDirectory()+"/" + "temp.xml"; try{ if(!lXmlfile.exists()) { lXmlfile.createNewFile(); } }catch(IOException e){ Log.e("IOException", e.toString()); return null; } Above code giving me IO exception Permission denied. Although I have added permission in AndroidManifest.xml like: still , can not create file. where am I going wrong? how can I test this on emulator (create file on which path?) .Pls help its urgent. One more question: Is user defined content provider is the best way to complete this requirement(saving and retrieving form data that also contains image). Pls share. Regards On Sep 26, 7:41 am, gjs wrote: > Hi, > > Seehttp://developer.android.com/reference/android/content/Context.html#g...() > > andhttp://developer.android.com/guide/topics/data/data-storage.html > > Regards > > On Sep 23, 7:28 pm, ksmobilejava wrote: > > > > > > > > > Hello , > > developing application withformhaving multiple fields, at the end > > user need to submit the information. > > On this event, all thedataneed to be stored inXMLfile. > > thusXMLwrite is required. plus when user asks for particular id then > > thedatafilled by that id is also to be displayed thusXMLread is > > required.(search operation) > > Following is the code I tried for generating samplexml. > > /// > > // > > FileOutputStream fileos = null; > > try{ > > > > fileos=openFileOutput("newxml.xml",Context.MODE_PRIVATE); > > Log.v("info","fileoutput open"); > > // fileos = new > > FileOutputStream(newxmlfile); > > }catch(FileNotFoundException e){ > > Log.e("FileNotFoundException", > > "can't create > > FileOutputStream"); > > } > > // create a XmlSerializer in order to > > writexmldata > > XmlSerializer serializer > > =Xml.newSerializer(); > > try { > > //we set the FileOutputStream as > > output for the > > serializer, using UTF-8 encoding > > > > serializer.setOutput(fileos, "UTF-8"); > > //Write > with encoding (if encoding not null) > > and standalone flag (if standalone not null) > > > > serializer.startDocument(null, Boolean.valueOf(true)); > > //set indentation option > > > > //serializer.setFeature("http://xmlpull.org/v1/doc/ > > features.html#indent-output", true); > > //start a tag called "root" > > serializer.startTag(null, > > "root"); > > //i indent code just to > > have a view similar toxml-tree > > > > serializer.startTag(null, "child1"); > > > > serializer.endTag(null, "child1"); > > > > > serializer.startTag(null, "child2"); > > //set an attribute > > called "attribute" with a "value" for > > > > > > serializer.attribute(null, "attribute", "value"); > > > > serializer.endTag(null, "child2"); > > > > > serializer.startTag(null, "child3"); > > //write some text > > inside > > > > serializer.text("some text inside child3"); > > > > serializer.endTag(null, "child3"); > > >
[android-developers] Re: Saving form data to XML
Hi, See http://developer.android.com/reference/android/content/Context.html#getFilesDir() and http://developer.android.com/guide/topics/data/data-storage.html Regards On Sep 23, 7:28 pm, ksmobilejava wrote: > Hello , > developing application with form having multiple fields, at the end > user need to submit the information. > On this event, all the data need to be stored in XML file. > thus XML write is required. plus when user asks for particular id then > the data filled by that id is also to be displayed thus XML read is > required.(search operation) > Following is the code I tried for generating sample xml. > /// > // > FileOutputStream fileos = null; > try{ > > fileos=openFileOutput("newxml.xml",Context.MODE_PRIVATE); > Log.v("info","fileoutput open"); > // fileos = new > FileOutputStream(newxmlfile); > }catch(FileNotFoundException e){ > Log.e("FileNotFoundException", "can't > create > FileOutputStream"); > } > // create a XmlSerializer in order to write > xml data > XmlSerializer serializer = > Xml.newSerializer(); > try { > //we set the FileOutputStream as > output for the > serializer, using UTF-8 encoding > serializer.setOutput(fileos, > "UTF-8"); > //Write with encoding (if encoding not null) > and standalone flag (if standalone not null) > > serializer.startDocument(null, Boolean.valueOf(true)); > //set indentation option > > //serializer.setFeature("http://xmlpull.org/v1/doc/ > features.html#indent-output", true); > //start a tag called "root" > serializer.startTag(null, > "root"); > //i indent code just to have > a view similar to xml-tree > > serializer.startTag(null, "child1"); > > serializer.endTag(null, "child1"); > > > serializer.startTag(null, "child2"); > //set an attribute > called "attribute" with a "value" for > > > serializer.attribute(null, "attribute", "value"); > > serializer.endTag(null, "child2"); > > > serializer.startTag(null, "child3"); > //write some text > inside > serializer.text("some > text inside child3"); > > serializer.endTag(null, "child3"); > > serializer.endTag(null, > "root"); > serializer.endDocument(); > //write xml data into the > FileOutputStream > serializer.flush(); > //finally we close the file > stream > fileos.close(); > > //TextView tv = > (TextView)this.findViewById(R.id.result); > //tv.setText("file has been > created on SD card"); > Log.v("info","file has been > created on SD card"); > return true; > } catch (Exception e) { > Log.e("Exception","error > occurred while creating xml file"); > return false; > } > /// > /// > executed this with actual device but, no new xml file is created. > where to store this programatically created xml file? > On emulator if I want to test this application, where the xml file > will get stored? > Pls guide me whats going wrong? > Thanks. -- You received