微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

webapp android studio 不下载pdf

如何解决webapp android studio 不下载pdf

我在 Android Studio WebView 中为平板电脑分配了一个 Angular Web 系统,我正在使用 pdfMake 生成 pdf。当我在平板电脑上的导航器 Google Chrome 上打开相同的 Angular 网络系统时,它确实可以毫无问题地下载 pdf。 问题是从 webApp 下载时。它不下载任何 pdf 并且在 Android Studio 控制台中显示错误是:

I/chromium: [INFO:CONSOLE(1)] "ERROR Open PDF in new window blocked by browser",source: http://IP/main.8e0e0c6656730f738673.js 

我在 MainActivity.kt 上的代码

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val miVisorWeb = findViewById<WebView>(R.id.visorWeb)
        val ajustes = miVisorWeb.settings
        ajustes.javaScriptEnabled = true
        ajustes.setAppCacheEnabled(true)
        ajustes.loadsImagesAutomatically = true
        ajustes.blockNetworkLoads = false
        ajustes.blockNetworkImage = false
        ajustes.allowFileAccess = true
        ajustes.allowUniversalAccessFromFileURLs = true
        ajustes.javaScriptCanopenWindowsAutomatically = true
        ajustes.domStorageEnabled = true
        miVisorWeb.webViewClient = MyClient()
        miVisorWeb.setDownloadListener({ url,userAgent,contentdisposition,mimeType,contentLength ->
            val request = DownloadManager.Request(Uri.parse(url))
            request.setMimeType(mimeType)
            request.addRequestHeader("cookie",CookieManager.getInstance().getCookie(url))
            request.addRequestHeader("User-Agent",userAgent)
            request.setDescription("Downloading file...")
            request.setTitle(URLUtil.guessFileName(url,mimeType))
            request.allowScanningByMediaScanner()
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
            request.setDestinationInExternalFilesDir(this@MainActivity,Environment.DIRECTORY_DOWNLOADS,".pdf")
            val dm = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
            dm.enqueue(request)
            Toast.makeText(applicationContext,"Downloading File",Toast.LENGTH_LONG).show()
        })
        miVisorWeb.loadUrl(url)
    }

    class MyClient : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView,Url: String): Boolean {
            view.loadUrl(Url)
            return true

        }
    }


    override fun onKeyDown(keyCode: Int,event: KeyEvent): Boolean {
        val miVisorWeb: WebView
        miVisorWeb = findViewById<View>(R.id.visorWeb) as WebView
        if (event.action == KeyEvent.ACTION_DOWN) {
            when (keyCode) {
                KeyEvent.KEYCODE_BACK -> {
                    if (miVisorWeb.canGoBack()) {
                        miVisorWeb.goBack()
                    } else {
                        finish()
                    }
                    return true
                }
            }
        }
        return super.onKeyDown(keyCode,event)
    }

我在 AndroidManifest.xml 上的代码

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.app.myapp">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/visorWeb"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> 

我更改了代码中的一些内容,所以现在我没有收到该错误,但也没有下载。 现在我拥有所有这些权限:

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>
    <uses-permission android:name="android.permission.BLUetoOTH" />

而 MainActivity.kt 的 onCreate 现在是这样的:

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val miVisorWeb = findViewById<WebView>(R.id.visorWeb)
        val ajustes = miVisorWeb.settings
        ajustes.javaScriptEnabled = true
        ajustes.setAppCacheEnabled(true)
        ajustes.loadsImagesAutomatically = true
        ajustes.blockNetworkLoads = false
        ajustes.blockNetworkImage = false
        ajustes.allowFileAccess = true
        ajustes.allowUniversalAccessFromFileURLs = true
        ajustes.javaScriptCanopenWindowsAutomatically = true
        ajustes.setSupportMultipleWindows(true)
        ajustes.domStorageEnabled = true
        miVisorWeb.webViewClient = MyClient()
        miVisorWeb.setDownloadListener({ url,contentLength ->
            val request = DownloadManager.Request(Uri.parse(url))
            request.setAllowednetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
            request.setMimeType(mimeType)
            request.addRequestHeader("cookie","cabornaFile.pdf")
            val dm = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
            dm.enqueue(request)
            Toast.makeText(applicationContext,Toast.LENGTH_LONG).show()
        })
        miVisorWeb.loadUrl(url)
    }

控制台显示

02/03 11:08:04: Launching 'app' on Physical Device.
Install successfully finished in 1 s 819 ms.
$ adb shell am start -n "com.appcaborna.koinsyscaborna/com.appcaborna.koinsyscaborna.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 10726 on device 'smart_products_connection_sa-gravity_octacore-J88E42016019662'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/.koinsyscaborn: Accessing hidden method Landroid/graphics/drawable/Drawable;->getopticalInsets()Landroid/graphics/Insets; (light greylist,linking)
    Accessing hidden field Landroid/graphics/Insets;->left:I (light greylist,linking)
W/.koinsyscaborn: Accessing hidden field Landroid/graphics/Insets;->right:I (light greylist,linking)
W/.koinsyscaborn: Accessing hidden field Landroid/graphics/Insets;->top:I (light greylist,linking)
    Accessing hidden field Landroid/graphics/Insets;->bottom:I (light greylist,linking)
W/.koinsyscaborn: Accessing hidden method Landroid/view/View;->getAccessibilityDelegate()Landroid/view/View$AccessibilityDelegate; (light greylist,linking)
W/.koinsyscaborn: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist,reflection)
W/.koinsyscaborn: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist,reflection)
I/WebViewFactory: Loading com.android.chrome version 88.0.4324.93 (code 432409323)
W/.koinsyscaborn: ClassLoaderContext classpath element mismatch for position 0. expected=base.apk!classes2.dex,found=/data/app/com.android.chrome-eSbDu7id_AwPLg9e_QScAA==/base.apk!classes2.dex (PCL[base.apk*4228874755:base.apk!classes2.dex*1923838886:base.apk!classes3.dex*4206463029] | PCL[/data/app/com.android.chrome-eSbDu7id_AwPLg9e_QScAA==/base.apk*4228874755:/data/app/com.android.chrome-eSbDu7id_AwPLg9e_QScAA==/base.apk!classes2.dex*1923838886:/data/app/com.android.chrome-eSbDu7id_AwPLg9e_QScAA==/base.apk!classes3.dex*4206463029])
W/.koinsyscaborn: ClassLoaderContext classpath element mismatch for position 0. expected=base.apk!classes2.dex,found=/data/app/com.android.chrome-eSbDu7id_AwPLg9e_QScAA==/base.apk!classes2.dex (PCL[base.apk*4228874755:base.apk!classes2.dex*1923838886:base.apk!classes3.dex*4206463029:split_autofill_assistant.apk*1654593765] | PCL[/data/app/com.android.chrome-eSbDu7id_AwPLg9e_QScAA==/base.apk*4228874755:/data/app/com.android.chrome-eSbDu7id_AwPLg9e_QScAA==/base.apk!classes2.dex*1923838886:/data/app/com.android.chrome-eSbDu7id_AwPLg9e_QScAA==/base.apk!classes3.dex*4206463029:/data/app/com.android.chrome-eSbDu7id_AwPLg9e_QScAA==/split_autofill_assistant.apk*1654593765])
I/WebViewFactory: SystemProperties GMsveRSION = 9_202001
    Get WebViewChromiumFactoryProviderForO
W/.koinsyscaborn: Accessing hidden method Landroid/os/Trace;->isTagEnabled(J)Z (light greylist,reflection)
    Accessing hidden method Landroid/os/Trace;->traceBegin(JLjava/lang/String;)V (light greylist,reflection)
    Accessing hidden method Landroid/os/Trace;->traceEnd(J)V (light greylist,reflection)
    Accessing hidden method Landroid/os/Trace;->asyncTraceBegin(JLjava/lang/String;I)V (light greylist,reflection)
    Accessing hidden method Landroid/os/Trace;->asyncTraceEnd(JLjava/lang/String;I)V (light greylist,reflection)
I/cr_LibraryLoader: Loaded native library version number "88.0.4324.93"
I/cr_CachingUmaRecorder: Flushed 4 samples from 4 histograms.
W/.app: Accessing hidden method Landroid/content/Context;->bindServiceAsUser(Landroid/content/Intent;Landroid/content/ServiceConnection;ILandroid/os/Handler;Landroid/os/UserHandle;)Z (light greylist,reflection)
W/.koinsyscaborn: Accessing hidden method Landroid/media/AudioManager;->getoutputLatency(I)I (light greylist,reflection)
I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColordisplay retrieved: 0
    android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRdisplay retrieved: 0
D/LongScreenshotUtil: register longscreenshotReceiver
D/Openglrenderer: Skia GL Pipeline
D/Openglrenderer: textureCacheSize 75497472
E/IMGSRV: :0: CheckProcessIsXiaoMi: 1529: found = 0
D/NetworkSecurityConfig: No Network Security Config specified,using platform default
I/Openglrenderer: Initialized EGL,version 1.4
D/Openglrenderer: Swap behavior 2
E/IMGSRV: :0: CheckProcessIsXiaoMi: 1529: found = 0
I/Openglrenderer: queryNativeWindowStatus 0x7e2f551010 query=0 dataspace=0
D/Openglrenderer: textureCacheSize 75497472
D/: Gralloc Register  w:800,h:1280,f:0x1,usage:0xb00,ui64Stamp:11953,sSize:4096000,line = 2106
D/: Gralloc Register  w:800,ui64Stamp:11972,ui64Stamp:11974,line = 2106
D/: Gralloc UnRegister  w:800,ui64Stamp:11974 line = 2218
D/: Gralloc UnRegister  w:800,ui64Stamp:11972 line = 2218
D/: Gralloc UnRegister  w:800,ui64Stamp:11953 line = 2218
D/LongScreenshotUtil: register longscreenshotReceiver
I/Openglrenderer: queryNativeWindowStatus 0x7e2f2d2010 query=0 dataspace=0
D/: Gralloc Register  w:800,ui64Stamp:13604,ui64Stamp:13613,ui64Stamp:13748,line = 2106

请某人¡帮助我!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。