android - 在 Androidx 上使用 registerForActivityResult 选择文件

标签 android kotlin android-intent androidx

我需要在我的 webviewActivity 中实现选择文件选项,我发现的所有教程都只有带有 startActivityResult 的示例,但它目前已被弃用,所以我想要一些关于如何将此代码转换为新模板的帮助如文档中所示: https://developer.android.com/training/basics/intents/result
WebviewActivity.kt

class WebviewActivity: AppCompatActivity() {
val REQUEST_SELECT_FILE = 1
val FILE_CHOOSER_RESULT = 2
var uploadMessage: ValueCallback<Array<Uri>>? = null
var uploaded: ValueCallback<Uri>? = null

private fun launchWebview(url: String): WebView =
    webview_id.apply{
        loadUrl(url)
        webViewClient : object = WebViewClient(){
            //...//
        }

        webChromeClient : object = WebChromeClient(){
            override fun onShowFileChooser(
                webView: WebView?,
                filePathCallback: ValueCallback<Array<Uri>>?,
                fileChooserParams: WebChromeClient.FileChooserParams
            ): Boolean{
                if (uploadMessage != null){
                    uploadMessage!!.onReceiveValue(null)
                    uploadMessage = null
                }

                uploadMessage = filePathCallback
                val intent = fileChooserParams.createIntent()

                startActivityForResult(intent, REQUEST_SELECT_FILE)

                return true
            }
        }
    }

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (requestCode == REQUEST_SELECT_FILE){
        uploadMessage!!.onReceiveValue(
            WebChromeClient.FileChooserParams.parseResult(
                resultCode, data
            )
        )
        uploadMessage = null
    } else if (requestCode == FILE_CHOOSER_RESULT){
        val result = if (data == null || resultCode != RESULT_OK) null else data.data
        uploaded!!.onReceiveValue(result)
        uploaded = null
    }
    super.onActivityResult(requestCode, resultCode, data)
}
}
我使用这个链接来制作上面的代码:Android File Chooser not calling from Android Webview

最佳答案

你必须做某事。像那样:

private fun createFile() {
  getResult.launch("chartName.pdf")
}

private val getResult = registerForActivityResult(
  CreateSpecificTypeDocument("application/pdf")
) { uri ->
  if(uri != null){
    contentResolver.openFileDescriptor(uri, "w")?.use {
      FileOutputStream(it.fileDescriptor).use { fileOutputStream ->
        //DO sth. with file
      }
    }
  }
}
和:
class CreateSpecificTypeDocument(private val type: String) :
  ActivityResultContracts.CreateDocument() {
  override fun createIntent(context: Context, input: String): Intent {
    return super.createIntent(context, input).setType(type)
  }
}

关于android - 在 Androidx 上使用 registerForActivityResult 选择文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68035953/

相关文章:

java - 导航到上一个 Activity

android - 如何在真实设备中查看我的应用程序的 sqlite 数据库的内容?

安卓 map 框 : How to unregister IntentReceiver?

android - 在 Android SDK 类上对 Kotlin 扩展功能进行单元测试

android - 如何使用 Intent.ACTION_VIEW 查看文件夹的内容?

android - 如何通过android中的音频端口获取数据

Android - Google+ 共享状态失败并且 Google Play 服务崩溃

java - Activity 类中的 getArguments() ?

java - 为什么 Kotlin 使用 == 来表示结构相等而引入 === 来表示引用相等

android - 如何在 Android 中使用 Kotlin 从 NavigationView 的 headerLayout 中指定的布局访问 View