For me, file upload isn\'t working in Android WebView
. I\'ve tried testing it on Android KitKat, Lollipop and newer versions but it\'s still not working.
He
This is the only one that worked for me,
This is the Kotlin code for the same.
Variables declaration :
private var mUploadMessage:ValueCallback<Uri>? = null
var uploadMessage:ValueCallback<Array<Uri>>? = null
val REQUEST_SELECT_FILE = 100
Set this webchromeclient to your webview
webViewMainActivity?.webChromeClient = object:WebChromeClient() {
override fun onJsAlert(view: WebView, url: String, message: String, result: JsResult): Boolean {
Log.d("alert", message)
val dialogBuilder = AlertDialog.Builder(this@MainActivity)
dialogBuilder.setMessage(message)
.setCancelable(false)
.setPositiveButton("OK") { _, _ ->
result.confirm()
}
val alert = dialogBuilder.create()
alert.show()
return true
}
// 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)
}
}
onActivity Result Code -
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
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)
}
}
It's already answered here: File Upload in WebView
You can also use this webview: https://github.com/mgks/Android-SmartWebView