[英]How to read a pdf using WebView in Android Studio using Kotlin?
I have tried multiple times to read a pdf using WebView
but it only displays a blank screen.我曾多次尝试使用
WebView
但它只显示空白屏幕。 What should I do?我应该怎么办? Here is my code: MainActivity.kt
这是我的代码:MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var webView: WebView
@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView = findViewById(R.id.webview)
webView.settings.setJavaScriptEnabled(true)
webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
view?.loadUrl(url.toString())
return true
}
}
webView.loadUrl("https://mindorks.s3.ap-south-1.amazonaws.com/courses/MindOrks_Android_Online_Professional_Course-Syllabus.pdf")
}
}
activity_main.xml activity_main.xml
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
<TextView
android:id="@+id/infoTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Unfortunately, Android WebViews do not support viewing PDFs.不幸的是,Android WebViews 不支持查看 PDF。 Some older Android devices aren't even shipped with the ability to view PDFs and rely on the user downloading an application from the Play Store in order to do so.
一些较旧的 Android 设备甚至不具备查看 PDF 的能力,并且依赖于用户从 Play 商店下载应用程序才能做到这一点。 You'll likely need to download the PDF via its URL through some sort of client (I'd recommend Retrofit) as a steam or byte array.
您可能需要通过某种客户端(我建议改造)通过其 URL 下载 PDF 作为蒸汽或字节数组。 From there, you'll need to launch an implicit intent with the Uri set to the PDF's data and the type set to the "application/pdf" media type.
从那里,您需要启动一个隐式意图,将 Uri 设置为 PDF 的数据,并将类型设置为“application/pdf”媒体类型。 Once downloaded, please use the Android Sharesheet to query for any acceptable applications.
下载后,请使用Android Sharesheet查询任何可接受的应用程序。 There are open source libraries which can aid in this process.
有一些开源库可以在这个过程中提供帮助。 But, I had this exact use case at a previous position, and I ended up implementing as described.
但是,我在之前的 position 中有这个确切的用例,我最终按照描述实现了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.