PDFViewer not displaying PDF fromStream

808 Views Asked by At

My goal

I want to retrieve the PDF file from my firebase database to display it inside my application.

Info

Library I used for PDF viewer:https://github.com/barteksc/AndroidPdfViewer Language I am using: Kotlin

Reference I used to do my code

How to use pdfviewer to Display PDF from Firebase in Android Studio https://www.geeksforgeeks.org/alternatives-for-the-deprecated-asynctask-in-android/

My Code

Below is the code that I used to fit my PDF file into the PDF Viewer:

import android.content.ContentValues.TAG
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.my.edu.tarc.bookgo.databinding.FragmentBookReadingPageBinding
import java.io.BufferedInputStream
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL
import java.util.concurrent.Executors


class FragmentBookReadingPage : Fragment() {

    private val myExecutor = Executors.newSingleThreadExecutor()
    private val myHandler = Handler(Looper.getMainLooper())

    private var _binding: FragmentBookReadingPageBinding?= null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        //View binding
        _binding = FragmentBookReadingPageBinding.inflate(layoutInflater,container,false)
        //Accept bundle data
        val args = this.arguments
        binding.bookTitle.text = args?.get("bookName").toString()

        //Fetch data from the firebase and fill into PDF View
        var inputStream: InputStream? = null
        myExecutor.execute {
            Log.d(TAG,args?.get("bookContent").toString())
            val url = URL(args?.get("bookContent").toString())
            val urlConnection: HttpURLConnection = url.openConnection() as HttpURLConnection
            if (urlConnection.responseCode == 200) {
                Log.d(TAG,"inputstream")
                inputStream = BufferedInputStream(urlConnection.inputStream)

            }
            myHandler.post {
                Log.d(TAG,"Post")
                Log.d(TAG,inputStream.toString())
                binding.bookPdf.fromStream(inputStream).pages(0).load()
            }
        }
        return binding.root
    }
}

My XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FragmentMainPage"
    tools:ignore="MissingDefaultResource">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/curved_solid_container"
        android:layout_margin="20dp"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:layout_margin="20dp"
            android:orientation="horizontal">
        <TextView
            android:id="@+id/book_title"
            android:layout_width="0dp"
            android:layout_weight="4"
            android:layout_height="wrap_content"
            android:text="Book Title"
            android:maxLines="2"
            android:ellipsize="end"
            android:textSize="20sp"
            android:textColor="@color/white"/>
            <Button
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:backgroundTint="@color/white"/>
            <Button
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_marginTop="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginBottom="5dp"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/white"/>
        </LinearLayout>
        <com.github.barteksc.pdfviewer.PDFView
            android:id="@+id/book_pdf"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"/>
    </LinearLayout>

</RelativeLayout>

My Log

enter image description here

Output Screen

enter image description here

I think this problem is probably caused by the inputStream but I am not sure how to fix it, does java.io.BufferedInputStream@3e867be7 means anything? Or do I write the PDFViewer wrongly? Any help is deeply appreciated.

0

There are 0 best solutions below