Monday, June 13, 2022
HomeWordPress DevelopmentLoad PDF From URL in Android with Kotlin

Load PDF From URL in Android with Kotlin


bundle com.gtappdevelopers.kotlingfgproject

  

import android.os.AsyncTask

import android.os.Construct

import android.os.Bundle

import androidx.annotation.RequiresApi

import androidx.appcompat.app.AppCompatActivity

import com.github.barteksc.pdfviewer.PDFView

import java.io.BufferedInputStream

import java.io.InputStream

import java.web.HttpURLConnection

import java.web.URL

import javax.web.ssl.HttpsURLConnection

  

class MainActivity : AppCompatActivity() {

      

    

    

    lateinit var pdfView: PDFView

  

    

  

    @RequiresApi(Construct.VERSION_CODES.LOLLIPOP)

    override enjoyable onCreate(savedInstanceState: Bundle?) {

        tremendous.onCreate(savedInstanceState)

        setContentView(R.structure.activity_main)

        

        

        pdfView = findViewById(R.id.idPDFView)

          

        

        

        

        

        RetrivePDFFromURL(pdfView).execute(pdfUrl)

    }

  

    

    

    

    class RetrivePDFFromURL(pdfView: PDFView) :

        AsyncTask<String, Void, InputStream>() {

          

        

        val mypdfView: PDFView = pdfView

  

        

        override enjoyable doInBackground(vararg params: String?): InputStream? {

            

            var inputStream: InputStream? = null

            strive {

                

                

                val url = URL(params.get(0))

                  

                

                val urlConnection: HttpURLConnection = url.openConnection() as HttpsURLConnection

                  

                

                

                

                if (urlConnection.responseCode == 200) {

                    

                    

                    inputStream = BufferedInputStream(urlConnection.inputStream)

                }

            }

            

            catch (e: Exception) {

                

                

                e.printStackTrace()

                return null;

            }

            

            return inputStream;

        }

  

        

        

        override enjoyable onPostExecute(outcome: InputStream?) {

            

            

            mypdfView.fromStream(outcome).load()

  

        }

    }

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments