[有一个应用程序,将用户入职流程实现为网页,并在嵌入式WebView中显示。流程步骤之一是上传文件。
通过使用网页上的<input/>
标记并通过覆盖的WebChromeClient.onShowFileChooser(..., filePathCallback: ValueCallback<Array<Uri>>?, ...)
进一步处理它,最终触发了应该由平台File-Chooser-Activity提供的startActivityForResult(...)
来实现(对于不同的供应商/版本)。
和往常一样,一切正常,直到活动被销毁:)
问题是,在应用程序活动的顶部启动File-Chooser-Activity时,最后一个活动可能会被销毁(由于缺少资源,轮换等,系统会将该活动销毁)。然后,我们拥有其filePathCallback
的WebView也将被销毁。这意味着应用程序在获取所选文件的URI(在filePathCallback
上)时,此时没有有效的onActivityResult
。
您如何解决此问题?
我创建了一个简单的项目来单独表示问题,请随时查看:https://github.com/allco/WebViewFileChooseIssue
这里有一些视频:
http://www.youtube.com/watch?v=9mnd0lT9lZI
案例2演示了问题,即使选择完成,应用仍然显示“未选择文件”。
这里是带有项目密钥文件的剪切片段:
class MainActivity : AppCompatActivity() {
companion object {
const val REQ_CODE_CHOOSER = 1
}
val unencodedHtml = "<input type=file>"
var webViewFileChooseCallback: ValueCallback<Array<Uri>>? = null
private val webClient = object : WebViewClient() {
...
}
private val chromeClient = object : WebChromeClient() {
...
override fun onShowFileChooser(
webView: WebView,
filePathCallback: ValueCallback<Array<Uri>>?,
fileChooserParams: FileChooserParams?
): Boolean {
if (filePathCallback == null) return false
webViewFileChooseCallback = filePathCallback
startFileChooserActivity("*/*")
return true
}
}
fun startFileChooserActivity(mimeType: String) {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = mimeType
intent.addCategory(Intent.CATEGORY_OPENABLE)
// special intent for Samsung file manager
val sIntent = Intent("com.sec.android.app.myfiles.PICK_DATA")
// if you want any file type, you can skip next line
sIntent.putExtra("CONTENT_TYPE", mimeType)
sIntent.addCategory(Intent.CATEGORY_DEFAULT)
val chooserIntent: Intent
if (packageManager.resolveActivity(sIntent, 0) != null) {
// it is device with Samsung file manager
chooserIntent = Intent.createChooser(sIntent, "Open file")
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arrayOf(intent))
} else {
chooserIntent = Intent.createChooser(intent, "Open file")
}
try {
startActivityForResult(chooserIntent, REQ_CODE_CHOOSER)
} catch (ex: android.content.ActivityNotFoundException) {
Toast.makeText(applicationContext, "No suitable File Manager was found.", Toast.LENGTH_SHORT).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val webView = findViewById<WebView>(R.id.webView)
webView.webChromeClient = chromeClient
webView.webViewClient = webClient
if (savedInstanceState == null) {
val encodedHtml = Base64.encodeToString(unencodedHtml.toByteArray(), Base64.NO_PADDING)
webView.loadData(encodedHtml, "text/html", "base64")
} else {
webView.restoreState(savedInstanceState)
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
findViewById<WebView>(R.id.webView).saveState(outState)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when {
requestCode == REQ_CODE_CHOOSER && resultCode == Activity.RESULT_OK && data != null -> {
val uri = when {
data.dataString != null -> arrayOf(Uri.parse(data.dataString))
data.clipData != null -> (0 until data.clipData!!.itemCount)
.mapNotNull { data.clipData?.getItemAt(it)?.uri }
.toTypedArray()
else -> null
}
webViewFileChooseCallback?.onReceiveValue(uri)
}
else -> super.onActivityResult(requestCode, resultCode, data)
}
}
}
将相关数据存储在onSaveInstanceState()
中并在onCreate()
中读取]