Kotlin WebView(onShowFileChooser)未将图像上传到数据库

时间:2018-10-08 10:46:39

标签: android file-upload webview kotlin

我无法将图像上传到我的代码数据库中 我该如何解决这个问题

import android.content.Intent
import android.net.Uri
import android.webkit.*
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.KeyEvent
import android.webkit.WebView
import android.webkit.WebChromeClient
import android.support.annotation.RequiresApi
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val ckinternet = checkInternet()

        @Suppress("OverridingDeprecatedMember")
        class SaereenWebView : WebViewClient(){
            @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
            override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {

                if(ckinternet.isConnected()){
                    saereenView?.loadUrl(request?.url.toString())
                } else {
                    ckinternet.Aler(this@MainActivity)
                }
                return true
            }
            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {

                if(ckinternet.isConnected()){
                    saereenView?.loadUrl(url)
                } else {
                    ckinternet.Aler(this@MainActivity)
                }
                return true
            }
        }

        if(ckinternet.isConnected()){
            saereenView.restoreState(savedInstanceState)
            saereenView.webViewClient = SaereenWebView()
            saereenView.settings.javaScriptEnabled  = true
            saereenView.settings.builtInZoomControls = true
            saereenView.settings.displayZoomControls = true
            saereenView.settings.allowFileAccess = true
            saereenView.settings.allowFileAccessFromFileURLs = true
            saereenView.settings.setGeolocationEnabled(true)
            CookieManager.getInstance().acceptCookie()
            saereenView.webChromeClient = (object:WebChromeClient() {
                override fun onGeolocationPermissionsShowPrompt(origin: String?, callback: GeolocationPermissions.Callback?) {
                    callback!!.invoke(origin, true, false)
                }

                // onShowFileChooser
                override fun onShowFileChooser(view: WebView, filePathCallback:ValueCallback<Array<Uri>>, fileChooserParams:FileChooserParams):Boolean {
                    val intent = Intent(Intent.ACTION_GET_CONTENT)
                    intent.setType("image/*")
                    val PICKFILE_REQUEST_CODE = 100
                    startActivityForResult(intent, PICKFILE_REQUEST_CODE)
                    return false
                }
            })

            saereenView.settings.setRenderPriority(WebSettings.RenderPriority.HIGH)
            saereenView.settings.cacheMode = WebSettings.LOAD_NO_CACHE
            if (Build.VERSION.SDK_INT >= 19) {
                saereenView.setLayerType(View.LAYER_TYPE_HARDWARE, null)
            } else {
                saereenView.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
            }
            saereenView.loadUrl("https://test.dv")
        } else {
            ckinternet.Aler(this@MainActivity)
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if( saereenView != null ) {
            if( (keyCode == KeyEvent.KEYCODE_BACK) && (saereenView.canGoBack()) ) {
                saereenView.goBack()
                return true
            }
        }
        return super.onKeyDown(keyCode, event)
    }
}

0 个答案:

没有答案