0

I would like to move a file from the download folder to a folder in my app, I have seen that you can use the Files.move (source, destination) function, but I don't know how to get the source and destination path.

When y try

 String sdCard = Environment.getExternalStorageDirectory().toString();
 File ficheroPrueba = new File(sdCard + "/pau_alem18je_compressed.pdf");

 if(ficheroPrueba.exists())
           Log.v(TAG, "Hola")
 }

Despite having downloaded the file (it is seen in the android emulator in downloads) it does not print the log.v

Gorpe
  • 93
  • 1
  • 7
  • Try for [download directory](https://stackoverflow.com/questions/18383055/android-where-are-downloaded-files-saved/18383302#18383302) and for [in app folder](https://stackoverflow.com/questions/5527764/get-application-directory/5527883#5527883) – Dmytro Batyuk Sep 04 '19 at 21:17
  • I have tried it but there is no way, I have explained it in the main message (I have updated in main message) @DmytroBatyuk – Gorpe Sep 05 '19 at 13:55

1 Answers1

0

Use Environment.getExternalStorageDirectory() to get to the root of external storage (which, on some devices, is an SD card).

String sdCard = Environment.getExternalStorageDirectory().toString();

    // the file to be moved or copied
    File sourceLocation = new File (sdCard + "/sample.txt");

    // make sure your target location folder exists!
    File targetLocation = new File (sdCard + "/MyNewFolder/sample.txt");

    // just to take note of the location sources
    Log.v(TAG, "sourceLocation: " + sourceLocation);
    Log.v(TAG, "targetLocation: " + targetLocation);

    try {

        // 1 = move the file, 2 = copy the file
        int actionChoice = 2;

        // moving the file to another directory
        if(actionChoice==1){

            if(sourceLocation.renameTo(targetLocation)){
                Log.v(TAG, "Move file successful.");
            }else{
                Log.v(TAG, "Move file failed.");
            }

        }

        // we will copy the file
        else{

            // make sure the target file exists

            if(sourceLocation.exists()){

                InputStream in = new FileInputStream(sourceLocation);
                OutputStream out = new FileOutputStream(targetLocation);

                // Copy the bits from instream to outstream
                byte[] buf = new byte[1024];
                int len;

                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                in.close();
                out.close();

                Log.v(TAG, "Copy file successful.");

            }else{
                Log.v(TAG, "Copy file failed. Source file missing.");
            }

        }

    } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Suman Kumar Dash
  • 681
  • 5
  • 19
  • I have this sourceLocation: /storage/emulated/0/Download/pau_alem18je_compressed.pdf and this storageLocation /storage/emulated/0app/src/main/assets/pau_alem18je_compressed.pdf but the code doesn't move the pdf @SumanDash – Gorpe Sep 05 '19 at 09:25