1

I have just a full screen WebView with a web-page where user can navigate. I need to make available uploading files in webview, for example attach files in gmail or send files in messanger. The problem is that now file attaching buttons just don't react, for example in Google Photos:

enter image description here

I need to work them exactly like in browser when it proposes the user to choose application for file uploading through implicit Intent. I suppose that I should use implicit Intent but don't know how to implement it in webview.

I enabled some necessary permissions and even more:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CAMERA"/>

Please tell me what decision is up-to-date and works for application with single webView. Unfortunately none of decisions here helped, may be because they are way too old: File Upload in WebView

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)


        if (savedInstanceState != null) {
            // Restore the previous URL and history stack
            binding.webView.restoreState(savedInstanceState);
        }

        val myWebViewClient = MyWebViewClient(this)
        binding.webView.settings.javaScriptEnabled = true

        val currentUrl = myWebViewClient.sharedPref.getString(URL, "https://www.google.ru")

        Log.d(tag, "sharedPref value: ${myWebViewClient.sharedPref.getString(URL, "")}")
        if (currentUrl != null) {
            binding.webView.loadUrl(currentUrl)
        }
        binding.webView.webViewClient = myWebViewClient
    }

    override fun onBackPressed() {
        binding.webView.apply {
            if (canGoBack()) {
                goBack()
            } else {
                onBackPressed()
            }
        }
    }
}

MyWebViewClient class:

val tag = "CUSTOMTAG"

val SHARED_PREFS = "sharedPrefs"
val URL = "URL"

class MyWebViewClient(val context: Context) : WebViewClient() {

    val sharedPref = context.getSharedPreferences(
        SHARED_PREFS,
        AppCompatActivity.MODE_PRIVATE
    )


    override fun shouldOverrideUrlLoading(view: WebView?, url: String): Boolean {
        view?.let {
            it.loadUrl(url)
        }
        return false
    }

    override fun onPageFinished(view: WebView?, url: String?) {
        super.onPageFinished(view, url)

        with(sharedPref.edit()) {
            putString(URL, url)
            apply()
        }

        if (url != null) {
            sharedPref.getString(URL, "")?.let { Log.d(tag, "sharedPref: $it") }
        }
    }
}
faritowich
  • 67
  • 1
  • 10
  • I see no upload code. Where are you complaining about? – blackapps Feb 15 '22 at 22:42
  • And beside INTERNET permission there is no code that needs any other permission. What is it that you are asking? – blackapps Feb 15 '22 at 22:57
  • Sorry, seems I did not describe it clear enought. Uploading can be anywhere. Attaching files in gmail and so on. Now uploading buttons do not work at all, I need to make them work correctly like in browser. – faritowich Feb 16 '22 at 06:13
  • I also see no uploading buttons in your code. – blackapps Feb 16 '22 at 08:28
  • Uploading buttons are in web pages opened in the WebView. Any uploading buttons in the web pages don't work. Screenshot attached for example. I don't mean native Android buttons. – faritowich Feb 16 '22 at 09:18
  • You should post minimal html code with those buttons so people who try to help you can test it out. Elementary Watson.. – blackapps Feb 16 '22 at 09:21
  • I don't know actually:( But does it really matter? I mean my mobile browser doen't need exact HTML codes but it still handles any attach buttons at any web pages. Doesn't it mean that all these attach buttons at all web pages are the same? – faritowich Feb 16 '22 at 10:05
  • If you are to lazy to post requested code then what do you expect further of us? – blackapps Feb 16 '22 at 11:02
  • I don't know how to get HTML code of a button on the web page, how can I get it? – faritowich Feb 16 '22 at 11:04
  • You say that they are all the same. So write your own minimal html code with buttons and test with them first before you post it here. Do you expect from us that we do it for you? Further i see no buttons in your picture but menu items. – blackapps Feb 16 '22 at 11:09

0 Answers0