简体   繁体   English

如何使用 Z539A3A5859D24FB7B7BFFAZ129E74D6F 在 Android Studio 中使用 WebView 读取 pdf?

[英]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.

相关问题 如何使用 kotlin 在 android webview 中设置 setjavascriptenabled - how to set setjavascriptenabled in android webview using kotlin 如何使用 kotlin 在 Android Studio 中使用 Storage Access Storage 创建 pdf? - How can I create pdf using Storage Access Storage in Android studio using kotlin? 我们如何在 url 中使用 url 直接在 webview 中读取 pdf 文件 - How can we read pdf file directly in webview using url in the android emulator 如何从网址下载pdf,内部存储并打开它:使用kotlin的Android Studio - how to download pdf form a url, to internal storage and open it : Android studio using kotlin 使用WebView在Android中显示PDF - PDF display in Android using WebView 尝试使用 google drive 在 android 应用程序(使用 kotlin)webview 中显示在线 pdf 有时会产生错误 - Trying to display online pdf's in an android application (using kotlin) webview using google drive will occasionally produce errors 如何使用Kotlin在Android Studio中引用TimerPicker - How to reference a timerPicker in android studio using kotlin 如何使用 kotlin 在 Android Studio 中创建按钮 - How to create a button in Android Studio using kotlin 如何使用 Android Z539A3A5859D24FB7B129E74D61 在 WebView 中加载 URL? - How to load an URL inside a WebView using Android Kotlin? 如何在android中使用itext库阅读pdf - How to read a pdf using itext library in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM