I’m trying to retrieve a photo taken in my android application and display it in my webview project; my web pages are local file:///android_asset/
with this function snapet i can launch the camera
@JavascriptInterface
public void prendreUnePhoto() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
pickMedia.launch(intent);
}
// the callbak
ActivityResultLauncher<Intent> pickMedia =
registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result-> {
if (result.getResultCode() == RESULT_OK && result.getData() !=null) {
Log.d("PhotoPicker", "Selected URI: " + result.getData().getExtras());
Bundle bundle = result.getData().getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
b= (Bitmap) bundle.get("data");
} else {
Log.d("PhotoPicker", "No media selected");
}
});
Here i convert the photo taken in Base64
@JavascriptInterface
public String getPhoto() {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 10, stream);
byte[] bytesArray = stream.toByteArray();
String str = Base64.encodeToString(bytesArray, Base64.DEFAULT);
return str;
}
then from the webapp side (webview client) i can launch the camera (JAVASCRIPT SNIPPET)
but.addEventListener("click", () => {
v = Android.prendreUnePhoto();
Android.getPhoto();
});
I would like to know how i can display the photo taken by the camera directly on a div on my client (webview)
I’m oblige to create another button then I can call the function getPhoto() and by this way i can display the photo taken
verifier.addEventListener("click", () => {
var b = Android.getPhoto();
var src = "data:image/jpeg;base64," + b;
$("#img").append(`<img src="${src}"/>`);
console.log(src);
//alert(typeof b);
});