Download a file from a URL and save it in the download folder of the internal memory

0

I'm doing tests with an example about downloading files from a url and recording it in the internal memory, apparently it works well, the code manages to link to the url and read the file but I can not keep it in the internal memory, see if someone can tell me the correct procedure to get it stored in the internal memory of the device.

Greetings and thanks in advance.

here is the code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate ( savedInstanceState );
    setContentView ( R.layout.activity_main );
    new Thread ( new Runnable () {
        @Override
        public void run() {
            try {

                URL url = new URL("http://www.videotutoriales.es/android-xml/cursos.xml");
                //establecemos la conexión con el destino
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection ();
                //establecemos el método jet para nuestra conexión
                //el método setdooutput es necesario para este tipo de conexiones
                urlConnection.setRequestMethod ( "GET" );
                urlConnection.setDoOutput ( true );
                //por último establecemos nuestra conexión y cruzamos los dedos                     
asked by Ciberdrac 26.08.2018 в 22:35
source

1 answer

0

First I want to tell you that the directory Downloads is actually in the External storage directory, you can get the path by:

String directorioDownloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); 

To define the internal storage directory, instead of defining the external storage directory:

File SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(SDCardRoot,"ejemplo.txt");

use getFilesDir () to save it in the internal memory:

File file = new File(getFilesDir(),"ejemplo.txt");
  

getFilesDir () Return the route absolute to the directory in the   file system.

    
answered by 27.08.2018 в 16:56