How to download file in url Android 8 Oreo

How to download file in URL Android 8 Oreo.




new DownloadFileFromURL(position).execute(data.get(url));

private class DownloadFileFromURL extends AsyncTask<String, String, String> {
    private ProgressDialog p;
    private int pos = 0;

    public DownloadFileFromURL(int p) {
        pos = p;
    }

    @Override    protected void onPreExecute() {
        super.onPreExecute();
        p = new ProgressDialog(context);
        p.setMessage("Downloading 0%");
        p.setIndeterminate(false);
        p.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        p.setCancelable(false);
        p.show();
    }

    @Override    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
        p.setMessage("Downloading " + values[0] + "%");
    }

    @Override    protected String doInBackground(String... f_url) {
        int count;
        String filename = f_url[0];
        String[] parts = filename.split("/");
        String part2 = parts[parts.length - 1];
        Boolean isSDPresent = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        File myNewFolder = null;
        if (isSDPresent) {
            String newFolder = "/patelwala";
            String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
            myNewFolder = new File(extStorageDirectory + newFolder);
            if (myNewFolder.exists()) {
            } else {
                myNewFolder.mkdirs();
            }
        } else {
            String newFolder = "patelwala";
            myNewFolder = context.getDir(newFolder, Context.MODE_PRIVATE);
            if (!myNewFolder.exists()) {
                myNewFolder.mkdirs();
            }
        }
        try {
            System.out.println("Downloading");
            URL url = new URL(f_url[0]);

            URLConnection conection = url.openConnection();
            conection.connect();
            // getting file length            int lenghtOfFile = conection.getContentLength();
            // input stream to read file - with 8k buffer            InputStream input = new BufferedInputStream(url.openStream(), 8192);
            // Output stream to write file            OutputStream output = new FileOutputStream(myNewFolder + "/" + part2);
            byte data[] = new byte[1024];

            long total = 0;
            while ((count = input.read(data)) != -1) {
                //if (!Webservice.isCancelled) {                total += count;

                // writing data to file                publishProgress("" + (int) ((total * 100) / lenghtOfFile));
                output.write(data, 0, count);
              /*  } else {                    break;                }*/
            }
            // flushing output            output.flush();
            // closing streams            output.close();
            input.close();

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

        return myNewFolder + "/" + part2;
    }


    /**     * After completing background task     **/    @Override    protected void onPostExecute(String s) {
        p.dismiss();
        openDocument(s);
    }

}



public void openDocument(String name) {
    if (co == 0) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        File file = new File(name);
        String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
        String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (extension.equalsIgnoreCase("") || mimetype == null) {
            // if there is no extension or there is no definite mimetype, still try to open the file            intent.setDataAndType(Uri.fromFile(file), "text*//*");
        } else {
            intent.setDataAndType(Uri.fromFile(file), mimetype);
        }
        // custom_alert_dialog message for the intent        context.startActivity(Intent.createChooser(intent, "Choose an Application:"));
    } else {
        co = 0;

        File f = new File(name);
        Uri uri = Uri.parse("file://" + f.getAbsolutePath());
        Intent share = new Intent(Intent.ACTION_SEND);
        //  share.putExtra(Intent.EXTRA_STREAM, uri);        String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(f).toString());
        String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (extension.equalsIgnoreCase("") || mimetype == null) {
            // if there is no extension or there is no definite mimetype, still try to open the file            share.setType("text*//*");
        } else {
            share.setType(mimetype);
        }
        share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
        share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        context.startActivity(Intent.createChooser(share, "Share File"));

    }


}


EmoticonEmoticon