I am using this code and I am missing something, because almost everything is working, but I get a null in the data when the callback responds:
private inner class JavascriptInterface {
@android.webkit.JavascriptInterface
fun image_capture() {
val photoFileName = "photo.jpg"
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
var photoFile = getPhotoFileUri(photoFileName)
if (photoFile != null) {
fileProvider = FileProvider.getUriForFile(applicationContext, "com.codepath.fileprovider", photoFile!!)
intent.putExtra(EXTRA_OUTPUT, fileProvider)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
if (intent.resolveActivity(packageManager) != null) {
getContent.launch(intent)
}
}
}
}
val getContent = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
if (result.resultCode == Activity.RESULT_OK) {
val intent:Intent? = result.data // <- PROBLEM: data is ALWAYS null
}
}
My manifest snippet related to this looks like this:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and my fileprovider.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-files-path name="images" path="Pictures" />
</paths>
Any help is appreciated. Thanks!
So, I ended up checking out the TakePicture contract @ian (thanks for that tip!) and after a lot of cobbling together various resources I found, I finally got it to work. This is the pertinent kotlin code for the webview Activity:
To round things out, here is the pertinent JavaScript code - which the Activity is loading via the myWebView.loadUrl(storedurl) statement.
This is the JavaScript code which calls the Android code:
And when the picture has been taken, and sized by the Android code, it sends the Base64 back to JavaScript with:
Note how weirdly you have to specify function arguments. There probably is a better way, but this code works. If there are any suggestions about how to specify a function argument easier than this, please let me know.
AndroidManifest.xml
And the provider_paths.xml
Hope this helps someone - it took me days of research to figure this one out!