title: how to download pdf form a url, to internal storage and open it : Android studio using kotlin
body: <pre><code>class Eco9 : AppCompatActivity() {
  
      lateinit internal var uri: Uri
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_eco9)
          this.getActionBar();
          this.supportActionBar!!.title = "Class 9th Economics";
  
          fab.setOnClickListener { view -&gt;
              Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                      .setAction("Action", null).show()
          }
          supportActionBar?.setDisplayHomeAsUpEnabled(true)
  
         val openActivityDownload: Button = findViewById(R.id.eco9ch1)
          openActivityDownload.setOnClickListener {
              val s = "https://drive.google.com/file/d/0B71LXrqWr0mFUTk5WnVyVEQ3MFE/export?format=pdf"
              val fname = "123.pdf"
              if (FileExists(fname)) { previewpdf(fname) }
              else { download(s)
                  Toast.makeText(applicationContext, "File will download", Toast.LENGTH_LONG).show()
              }
          }
  
      }
  
      fun FileExists(name: String): Boolean {
          val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).path + separator + name)
          return file.exists()
      }
  
      private fun download(s: String) {
          val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
          val uri = Uri.parse(s)
          val request = DownloadManager.Request(uri)
          val nameOfFile = URLUtil.guessFileName(uri.toString(), null, MimeTypeMap.getFileExtensionFromUrl(uri.toString()))
          val destinationInExternalPublicDir = request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS)
          request.setAllowedOverMetered(true)
          request.setAllowedOverRoaming(true)
          request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
          request.allowScanningByMediaScanner()
          downloadManager.enqueue(request)
      }
  
      private fun previewpdf(name: String) {
         val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).path + separator + name)
         val path = fromFile(file)
          intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
          val intent = Intent(Intent.ACTION_VIEW, path)
  
          val chooser = Intent.createChooser(intent, "Open with")
          if (intent.resolveActivity(packageManager) != null)
              startActivity(chooser)
          else
              Toast.makeText(applicationContext, "No suitable application to open file", Toast.LENGTH_LONG).show()
      }
  }
  </code></pre>
  
  <p><strong>I want to download a pdf from given url (s),</strong> I am pressing button named "echo9ch1",
  and calling function download(s) if file is not present in internal public download folder and otherwise if it is present, it should open pdf, not download it again. Where am i missing ? I can open 123.pdf, which was downloaded earlier. but I deleted my pdf file and <strong>download function is not working now</strong>. Actually a small problem came, as i was playing with code in download function.Help me with <strong>download()</strong> function, and see preview() and fileExists() function also.
  I added permissions in manifest.xml also. And I want to download pdf without wifi also.</p>
  
  <p>Manifest permissions are :</p>
  
  <pre><code> &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
  </code></pre>