Open file in SDCard on Android

–1 vote

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 need to open a file in my SDCard, but i can't open it
Jun 19, 2022 in Others by polo
• 1,480 points
40,132 views

1 answer to this question.

+1 vote

To open a file that is on the External Storage or the SD card:

 File root = new File(Environment.getExternalStorageDirectory(), "Your folder");
 File gpxfile = new File(root, "xml_nova_contagem.xml");

Remember that if your app is working with the SD card you need to manage the scenarios where the SD card is not mounted, etc. 

If your xml document works as database, or you are going to save critical information here, I suggest that you should create this xml file in the Internal Storage.

answered Jun 20, 2022 by nisha
• 2,210 points

Related Questions In Others

0 votes
0 answers

Is it possible to open developer tools console in Chrome on Android phone?

Is it possible to open JS console ...READ MORE

Jun 14, 2022 in Others by nisha
• 2,210 points
1,493 views
0 votes
1 answer

Open Instagram user profile on android

To open instagram app- String scheme = "http://instagram.com/_u/USER"; String path ...READ MORE

answered May 31, 2022 in Others by nisha
• 2,210 points
2,170 views
0 votes
1 answer

How to open YouTube video link in android app?

The below code will do that: Intent intent ...READ MORE

answered Jun 1, 2022 in Others by nisha
• 2,210 points
2,470 views
0 votes
1 answer

How can i create file at external storage in Android?

We have not had read/write filesystem access ...READ MORE

answered Jun 6, 2022 in Others by nisha
• 2,210 points
3,479 views
0 votes
0 answers

Open Contact information in Contact List using Phone Number of Contact Android Studio

I am making an app that has ...READ MORE

Jun 7, 2022 in Others by polo
• 1,480 points
758 views
0 votes
1 answer

Android Open External Storage directory for storing file

To get the internal SD card you ...READ MORE

answered Jun 9, 2022 in Others by nisha
• 2,210 points
1,680 views
0 votes
1 answer

open link of google play store in mobile version android

You can use Android Intents library for opening your ...READ MORE

answered Jun 9, 2022 in Others by nisha
• 2,210 points
1,625 views
0 votes
1 answer

Download file from URL in Excel 2019 (it works on Excel 2007)

The Sub Code looks fine. Check the ...READ MORE

answered Oct 7, 2022 in Others by narikkadan
• 63,600 points
1,803 views
0 votes
1 answer

How use File("/sdcard/myfile") without READ_EXTERNAL_STORAGE on android 11?

You first need to use File.exists() and ...READ MORE

answered May 31, 2022 in Others by nisha
• 2,210 points
1,147 views
0 votes
1 answer

Android Open External Storage directory(sdcard) for storing file

you can't do that. A workaround is to ...READ MORE

answered Jun 9, 2022 in Others by nisha
• 2,210 points
2,634 views
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