显示用于选择图像或通过网络视图从相机中选择的选项

时间:2019-07-20 11:23:07

标签: android kotlin webview

我正在使用android的webview来显示一个网站,该网站具有从图库上传文件或单击相机的选项!该网站在谷歌浏览器中显示如下提示: enter image description here 这是我在Kotlin中webview的代码!到目前为止,我已经能够打开文件选择器,但是它不会上传图像文件。

class MainActivity : AppCompatActivity() {

    private lateinit var myWebView: WebView
    private lateinit var toolbar: Toolbar
    private lateinit var reloadButton: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        myWebView = findViewById(R.id.webview)
        toolbar = findViewById(R.id.my_toolbar)
        reloadButton = findViewById(R.id.reload_button)
        setSupportActionBar(toolbar)

        toolbar.title = "Halal Rishtey"
        toolbar.setBackgroundColor(Color.rgb(219, 120, 120))
        toolbar.setTitleTextColor(Color.WHITE)
        myWebView.settings.allowFileAccess = true
        myWebView.settings.allowContentAccess = true
        myWebView.settings.javaScriptCanOpenWindowsAutomatically = true
        myWebView.settings.allowFileAccessFromFileURLs = true
        myWebView.settings.javaScriptCanOpenWindowsAutomatically = true
        myWebView.settings.javaScriptEnabled = true
        myWebView.settings.databaseEnabled = true
        myWebView.settings.setAppCacheEnabled(true)

        myWebView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
                view?.loadUrl(request?.url.toString())
                return true
            }
        }

        myWebView.webChromeClient = object : WebChromeClient() {
            override fun onShowFileChooser(
                webView: WebView,
                filePathCallback: ValueCallback<Array<Uri>>,
                fileChooserParams: FileChooserParams
            ): Boolean {
                var mFilePathCallback = filePathCallback
                val intent = Intent(Intent.ACTION_GET_CONTENT)
                intent.type = "*/*"
                val PICKFILE_REQUEST_CODE = 100
                startActivityForResult(intent, PICKFILE_REQUEST_CODE)
                return true
            }
        }

        fun onActivityResult(
            requestCode: Int, resultCode: Int,
            intent: Intent,
            mFilePathCallback: Any
        ): Boolean {
            var PICKFILE_REQUEST_CODE = null
            if (requestCode == PICKFILE_REQUEST_CODE) {
                val result = if (intent == null || resultCode != RESULT_OK)
                    null
                else
                    intent.getData()
                val resultsArray = arrayOfNulls<Uri>(1)
                resultsArray[0] = result
                mFilePathCallback.onReceiveValue(resultsArray)

            }
            return true
        }

        myWebView.setDownloadListener(object : DownloadListener {
            override fun onDownloadStart(
                url: String, userAgent: String,
                contentDisposition: String, mimetype: String,
                contentLength: Long
            ) {
                val request = DownloadManager.Request(Uri.parse(url))
                request.allowScanningByMediaScanner()

                request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) //Notify client once download is completed!
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, mimetype)
                val webview = getSystemService(DOWNLOAD_SERVICE) as DownloadManager
                webview.enqueue(request)
                Toast.makeText(applicationContext, "Downloading File", Toast.LENGTH_LONG).show()
            }
        })

        myWebView.loadUrl("https://halalrishtey.com/")

        reloadButton.setOnClickListener {
            myWebView.reload()
        }
    }

    override fun onBackPressed() {
        super.onBackPressed()
        if (myWebView.canGoBack()) {
            myWebView.goBack()
        }
    }

    private fun Any.onReceiveValue(resultsArray: Array<Uri?>) {}
}

如何通过android的webview显示类似的提示?

0 个答案:

没有答案