Open file in SDCard on Android

0 votes
I have written the code below, but I keep receiving errors about Path Separator.

btn_guardar.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {

            final String CP7_String = et_id.getText().toString();
            final String ncontagem_String = et_ncontagem.getText().toString();

        Thread thread = new Thread () {
                public void run() {

                      String PATH=null ;

                            FileOutputStream fileos = null;
                            try{

                                    if(fileos==null)
                                {

                                    // If the file don't exists

                                    File file = new File(Environment.getExternalStorageDirectory().toString()+"/xml_nova_contagem.xml" );

                                    fileos = new FileOutputStream(file);

                                }
                                        // If the file exists

                        /////////-------- THIS METHOD WON'T WORK --------///////

                                    /*  if (fileos!=null){

                                           System.out.println("aqui vai dar bronca: " + Environment.getExternalStorageDirectory().toString());

                                    //     PATH = Environment.getExternalStorageDirectory().toString() + "/xml_nova_contagem.xml";
                                           Context context= getApplicationContext();

                                           fileos = context.openFileOutput("xml_nova_contagem.xml", Context.MODE_APPEND);

                                           System.out.println("okkkkkkkkk ");
                                            }
                                             */

                            XmlSerializer serializer = Xml.newSerializer();
                            try{

                                serializer.setOutput(fileos, "iso-8859-1");
                                serializer.startDocument(null, Boolean.valueOf(true));

                                serializer.startTag(null, "CodigoPostal");
                                serializer.startTag(null, "CP7");
                                serializer.text(CP7_String);
                                serializer.endTag(null,"CP7");
                                serializer.startTag(null,"NovaContagem");
                                serializer.text(ncontagem_String);
                                serializer.endTag(null, "NovaContagem");
                                serializer.endTag(null, "CodigoPostal");
                                serializer.endDocument();
                                serializer.flush();
                                fileos.close();

                                System.out.println("endddddddddda");

                                }catch(Exception ee)
                                {

                                    System.out.println("Exception"+ee);
                                }

                        }
                    catch (Exception e) {

                        System.out.println("Erro a escrever"+e);
                        System.out.println("path"+PATH);
                    }

               }

                    };thread.start();

        }
    });

}

}

I require opening a file on my SDCard, however, I am unable to do so. It is intended to add new records to an existing XML file, but since I am unable to read it, I am unable to do so. This approach won't work, but I have another one that creates the XML file and adds the record, and it does.
Oct 10, 2022 in Android by Edureka
• 13,620 points

edited Mar 4 1 view

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP