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

1 answer to this question.

0 votes

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. look this : Writing Text File to SD Card fails

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 :

http://developer.android.com/guide/topics/data/data-storage.html

answered Jun 20 by nisha
• 2,180 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 in Others by nisha
• 2,180 points
17 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 in Others by nisha
• 2,180 points
24 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 in Others by nisha
• 2,180 points
63 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 in Others by nisha
• 2,180 points
113 views
0 votes
0 answers
0 votes
1 answer
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 in Others by nisha
• 2,180 points
40 views
0 votes
1 answer

Get files in my own file format for its own dynamic icon

Regarding MAC support, "The Mac and Windows ...READ MORE

answered Jan 8, 2019 in Others by DataKing99
• 8,240 points
568 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 in Others by nisha
• 2,180 points
27 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP