Veg*_* ZA 1 android webview android-camera android-camera-intent kotlin
我已经尝试过这个并且也测试了这个代码。如果我需要从文件浏览器上传,两者都可以正常工作,但我需要可以在文件浏览器或相机之间进行选择。
我可以找到很多 Java 示例来执行此操作,但我找不到使用 Kotlin 完成的工作版本。下面是我当前的webview 文件上传器代码:
活动结果:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
if(requestCode == REQUEST_SELECT_FILE){
if(uploadMessage != null){
uploadMessage?.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode,data))
uploadMessage = null
}
}
}else if(requestCode == FILECHOOSER_RESULTCODE){
if(mUploadMessage!=null){
var result = data?.data
mUploadMessage?.onReceiveValue(result)
mUploadMessage = null
}
}else{
Toast.makeText(this,"Failed to open file uploader, please check app permissions.",Toast.LENGTH_LONG).show()
super.onActivityResult(requestCode, resultCode, data)
}
Run Code Online (Sandbox Code Playgroud)
设置WebChrome客户端:
// For 3.0+ Devices (Start)
// onActivityResult attached before constructor
fun openFileChooser(uploadMsg : ValueCallback<Uri>, acceptType:String) {
mUploadMessage = uploadMsg
val i = Intent(Intent.ACTION_GET_CONTENT)
i.addCategory(Intent.CATEGORY_OPENABLE)
i.type = "*/*"
startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE)
}
// For Lollipop 5.0+ Devices
override fun onShowFileChooser(mWebView:WebView, filePathCallback:ValueCallback<Array<Uri>>, fileChooserParams:WebChromeClient.FileChooserParams):Boolean {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
if (uploadMessage != null) {
uploadMessage?.onReceiveValue(null)
uploadMessage = null
}
uploadMessage = filePathCallback
val intent = fileChooserParams.createIntent()
try {
startActivityForResult(intent, REQUEST_SELECT_FILE)
} catch (e:ActivityNotFoundException) {
uploadMessage = null
Toast.makeText(getApplicationContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show()
return false
}
return true
}else{
return false
}
}
//For Android 4.1 only
fun openFileChooser(uploadMsg:ValueCallback<Uri>, acceptType:String, capture:String) {
mUploadMessage = uploadMsg
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "*/*"
startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE)
}
fun openFileChooser(uploadMsg:ValueCallback<Uri>) {
//filePermission()
mUploadMessage = uploadMsg
val i = Intent(Intent.ACTION_GET_CONTENT)
i.addCategory(Intent.CATEGORY_OPENABLE)
i.type = "*/*"
startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE)
}
Run Code Online (Sandbox Code Playgroud)
有人有允许通过相机或文件浏览器上传的工作代码示例吗?
此外,文件浏览器在选择存储在设备本身上的图像/pdf 时可以工作,但通过此方法从 Google 云端硬盘中获取的文件无法正确上传。有任何想法吗?
您可能缺少添加以下代码
webview.getSettings().setDomStorageEnabled(true);
webview.getSettings().setAllowContentAccess(true);
webview.getSettings().setAllowFileAccess(true);
Run Code Online (Sandbox Code Playgroud)
在这里我已经给出了详细的答案。希望对你有帮助。如果您有任何疑问,请告诉我。
编辑
全局声明下面提到的变量
var requiredPermissions = arrayOf<String>(Permissions.CAMERA, Permissions.WRITE_EXTERNAL_STORAGE, Permissions.READ_EXTERNAL_STORAGE/*, Permissions.WRITE_SETTINGS*/)
val REQUEST_SELECT_FILE = 100
private val FILECHOOSER_RESULTCODE = 1
var uploadMessage: ValueCallback<Array<Uri>>? = null
var link: String? = null
private var mUploadMessage: ValueCallback<*>? = null
Run Code Online (Sandbox Code Playgroud)
科特林代码:
@SuppressLint("SetJavaScriptEnabled")
private fun startWebView(url: String) {
// Create new webview Client to show progress dialog
// When opening a url or click on link
// Javascript enabled on webview
mWebView.settings.javaScriptEnabled = true
mWebView.settings.builtInZoomControls = true
mWebView.settings.displayZoomControls = true
mWebView.settings.domStorageEnabled = true
mWebView.settings.allowContentAccess = true
mWebView.settings.setAppCacheEnabled(false)
mWebView.settings.cacheMode = WebSettings.LOAD_NO_CACHE
mWebView.settings.setGeolocationEnabled(true) // life saver, do not remove
mWebView.addJavascriptInterface(WebAppInterface(this), "Android")
mWebView.webChromeClient = MyWebChromeClient()
mWebView.webViewClient = object : WebViewClient() {
// If you will not use this method url links are open in new browser
// not in webview
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
view.loadUrl(url)
return true
}
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
view.loadUrl(request.url.toString())
}
return true
}
override fun onReceivedError(view: WebView?, errorCode: Int, description: String?, failingUrl: String?) {
super.onReceivedError(view, errorCode, description, failingUrl)
util._log(TAG, "onReceivedError ")
}
// Show loader on url load
override fun onLoadResource(view: WebView, url: String) {
}
override fun onPageFinished(view: WebView, url: String) {
super.onPageFinished(view, url)
progressBar.visibility = View.GONE
}
override fun onReceivedHttpError(view: WebView?, request: WebResourceRequest?, errorResponse: WebResourceResponse?) {
super.onReceivedHttpError(view, request, errorResponse)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
util._log(TAG, "onReceivedHttpError ${errorResponse?.statusCode}")
}
}
override fun onReceivedError(view: WebView, request: WebResourceRequest, error: WebResourceError) {
super.onReceivedError(view, request, error)
util._log(TAG, "onReceivedError ")
WebViewClient.ERROR_AUTHENTICATION
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
util._log(TAG, "error code: ${error.errorCode} " + request.url.toString() + " , " + error.description)
}
}
override fun onReceivedSslError(view: WebView, handler: SslErrorHandler, error: SslError) {
super.onReceivedSslError(view, handler, error)
util._log(TAG, "SSl error ")
}
}
// Other webview options
/*
* mWebView.getSettings().setLoadWithOverviewMode(true);
* mWebView.getSettings().setUseWideViewPort(true);
* mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
* mWebView.setScrollbarFadingEnabled(false);
* mWebView.getSettings().setBuiltInZoomControls(true);
*/
// Load url in webview
if (NetworkStatus.isOnline(this)) {
Handler().postDelayed({ mWebView.loadUrl(url) }, 400)
} else {
util.showToast(this, getString(R.string.no_internet), true)
}
}
internal inner class MyWebChromeClient : WebChromeClient() {
// For 3.0+ Devices (Start)
// onActivityResult attached before constructor
protected fun openFileChooser(uploadMsg: ValueCallback<*>, acceptType: String) {
mUploadMessage = uploadMsg
val i = Intent(Intent.ACTION_GET_CONTENT)
i.addCategory(Intent.CATEGORY_OPENABLE)
i.type = "image/*"
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE)
}
// For Lollipop 5.0+ Devices
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
override fun onShowFileChooser(mWebView: WebView, filePathCallback: ValueCallback<Array<Uri>>, fileChooserParams: FileChooserParams): Boolean {
if (uploadMessage != null) {
uploadMessage!!.onReceiveValue(null)
uploadMessage = null
}
uploadMessage = filePathCallback
val intent = fileChooserParams.createIntent()
try {
startActivityForResult(intent, REQUEST_SELECT_FILE)
} catch (e: Exception) {
uploadMessage = null
util.showToast(this@WebLink, "Cannot Open File Chooser")
return false
}
return true
}
//For Android 4.1 only
protected fun openFileChooser(uploadMsg: ValueCallback<Uri>, acceptType: String, capture: String) {
mUploadMessage = uploadMsg
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "image/*"
startActivityForResult(Intent.createChooser(intent, "File Chooser"), FILECHOOSER_RESULTCODE)
}
protected fun openFileChooser(uploadMsg: ValueCallback<Uri>) {
mUploadMessage = uploadMsg
val i = Intent(Intent.ACTION_GET_CONTENT)
i.addCategory(Intent.CATEGORY_OPENABLE)
i.type = "image/*"
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6136 次 |
| 最近记录: |