I have tried this and have also tested this code. Both work correctly if I need to upload from the file browser but I need to have the option to choose between the File Browser or Camera.
There's plenty of Java examples that I can find that do this, but I cannot find a working version that's done with Kotlin. Here's my current code below for the webview file uploader:
onActivityResult:
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)
}
setWebChromeClient:
// 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)
}
Does anyone have a working sample of code that allows for uploading via the Camera or File Browser?
Also, the file browser works when selecting images/pdfs that's stored on the device itself but the files from Google Drive via this method, do not upload correctly. Any ideas?