pdfviewer不显示从流的PDF

发布于 2025-01-19 10:50:28 字数 5629 浏览 3 评论 0原文

我的目标

我想从我的Firebase数据库中检索PDF文件以在我的应用程序中显示。

info

库我用于PDF查看器: https://github.com/github.com/bartekscsc./androidpdpdfviewer- 我正在使用的语言:kotlin

参考我曾经做我的代码

如何使用PDFViewer在Android Studio In Android Studio中显示PDF https://wwwww.geeksforgeeks.org/alternatives-for - deprecated-assynctask in android/

我的代码

下面是我用来将我的pdf文件适合pdf查看器的代码:

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
    }
}

我的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>

我的log

”在此处输入图像描述“

输出屏幕

“在此处输入映像”

我认为这个问题可能是由InputStream引起的,但我不确定如何确定为了解决它,java.io.bufferedinputStream@3e867be7​​意味着什么?还是我错误地编写PDFViewer? 任何帮助都将得到深深的赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文