javaからKotlinに変換されたコードを見つけ組み込みました。
WebViewでUploadは可能ですが、1ファイルしか選択できません。
どなたか、複数ファイル対応の方法をご存知ないでしょうか。
よろしくお願い致します。
package jp.hogehoge.test
import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.webkit.*
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
private var mUploadMessage: ValueCallback<Uri>? = null
var uploadMessage:ValueCallback<Array<Uri>>? = null
val REQUEST_SELECT_FILE = 100
val FILECHOOSER_RESULTCODE = 1
class WebSite : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_web_site)
val myWebView: WebView = findViewById(R.id.webSiteView)
myWebView?.webChromeClient = object:WebChromeClient() {
override fun onJsAlert(view: WebView, url: String, message: String, result: JsResult): Boolean {
Log.d("alert", message)
val dialogBuilder = AlertDialog.Builder(this@WebSite)
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 = "image/jpeg"
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 = "image/jpeg"
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 = "image/jpeg"
startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE)
}
}
// これがないとwebView内でなくブラウザが起動してしまう
myWebView.setWebViewClient(object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
return false
}
})
myWebView.loadUrl("https://hogehoge.jp/login")
myWebView.settings.javaScriptEnabled = true
val goBackMainButton = findViewById(R.id.goBackMainBtn) as Button
goBackMainButton.setOnClickListener {
finish()
}
}
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)
}
}
}