-5
         try {
            URL url = new URL(file_url);
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            String PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/download";
            File file = new File(PATH);
            if(!file.exists()) {
                file.mkdirs(); 
            }
            String name=String.valueOf(code)+".mp4";
            File outputFile = new File(file,name);
            FileOutputStream fos = 
                     new FileOutputStream(outputFile);//this line

            InputStream is = c.getInputStream();
            int fileLength = sizeoffile;

            byte[] buffer = new byte[4096];
            int len1 = 0;
            long total = 0;
            while ((len1 = is.read(buffer)) != -1) {
                total += len1;
                if (fileLength > 0) 
                    publishProgress((int) (total));
                fos.write(buffer, 0, len1);
            }
            fos.close();
            is.close();

        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }

i want to download file with async class but it error when i rather to find the file path? this is a error in up to android 6? java.io.FileNotFoundException: /sdcard/downloadedfile.jpg: open failed: ENOENT (No such file or directory)

i added these permissions but doesn't work!

baloot
  • 49
  • 4
  • Does your application has the required permissions? Did you try doing some prior research? – GhostCat Aug 12 '17 at 09:30
  • do research about this you can find more resources to do u want or see this link https://stackoverflow.com/questions/9759205/download-a-pdf-file-and-save-it-to-sdcard-and-then-read-it-from-there – ND1010_ Aug 12 '17 at 09:35

1 Answers1

0

You first have to check if the directory exists or not if it doesn't exists then you have to make directory first

File dir = new File("path/to/your/directory");
try{
  if(dir.mkdir()) {
     System.out.println("Directory created");
  } else {
     System.out.println("Directory is not created");
  }
}catch(Exception e){
  e.printStackTrace();
}
Faizal Abbas
  • 205
  • 1
  • 10