pdfviewer不显示从流的PDF
我的目标
我想从我的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
Output Screen
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论