Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature/webview-image-upload] WebView의 FileChooser로 이미지 웹으로 전달 기능 구현 #817

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,55 @@
*/
package org.sopt.official.webview.view

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebView
import androidx.activity.addCallback
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import dagger.hilt.android.AndroidEntryPoint
import org.sopt.official.common.util.viewBinding
import org.sopt.official.webview.databinding.ActivityWebViewBinding

@AndroidEntryPoint
class WebViewActivity : AppCompatActivity() {
companion object {
const val INTENT_URL = "_intent_url"
}

private val binding by viewBinding(ActivityWebViewBinding::inflate)
private var filePathCallback: ValueCallback<Array<Uri>>? = null
private val imageResult = registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri: Uri? ->
if (uri == null) {
filePathCallback?.onReceiveValue(null)
filePathCallback = null
} else {
val data = Intent().apply { data = uri }
filePathCallback?.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(Activity.RESULT_OK, data))
filePathCallback = null
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.webView.webChromeClient = object : WebChromeClient() {
override fun onShowFileChooser(
webView: WebView?,
filePathCallback: ValueCallback<Array<Uri>>?,
fileChooserParams: FileChooserParams?
): Boolean {
if (filePathCallback != null) {
[email protected] = filePathCallback
}

imageResult.launch(
PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly)
)
return true
}
}
handleLinkUrl()
handleOnBackPressed()
handleOnPullToRefresh()
Expand All @@ -68,4 +98,8 @@ class WebViewActivity : AppCompatActivity() {
binding.webView.reload()
}
}

companion object {
const val INTENT_URL = "_intent_url"
}
}