dears I am working on and android app the app consist of a web view that I am displaying a web site and at some button at the website when pressed I am opining the camera of the device in order to capture an image then onActiveResult I a trying to capture the bitmap then evalute java script in order to add this image to website the code of opening the camera is like below:
@JavascriptInterface
public void takePhoto(String elementId){
// Toast.makeText(context, "Here in camera", Toast.LENGTH_SHORT).show();
this.elementId = elementId;
imageHandler = new ImageHandler(context);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
imageUri = context.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
((AppCompatActivity) context).startActivityForResult(intent, REQUEST_TAKE_PHOTO);
}
and the code to get the bitmap is like below;
if(requestCode == 500 && resultCode == RESULT_OK)
{
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(
context.getContentResolver(), imageUri);
ContentResolver contentResolver = context.getContentResolver ();
contentResolver.delete (imageUri,null ,null );
} catch (Exception e) {
e.printStackTrace();
}
if(bitmap == null) {
int width = Math.round((float) 0.2 * bitmap.getWidth());
int height = Math.round((float) 0.2 * bitmap.getHeight());
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, width,
height, false);
String base64Image = imageHandler.convertBase64(newBitmap);
base64Image = base64Image.replace("\n", "");
String s1 = "url('data:image/jpeg;base64," + base64Image + "') no-repeat 0% 0% / 100% 100% white";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webView.evaluateJavascript("javascript:" +
"$('[data-key=\"" + getElementId() + "\"] .Preview').css('background',\"" + s1 + "\");"
, null);
webView.evaluateJavascript("$('[data-key=\"" + getElementId() + "\"] input').val(\"" + base64Image + "\")", null);
}
}else{
Log.d("kiki", " null bitmap");
}
}
}
the code run smoothly on android 9 but at android 10 a weird behavior happen, after capturing the image and pressing ok and supposedly getting back to the onActiveResult the app seems to crash and the run unplugged therefore no helpful log appear firstly I noticed the the bitmap sometimes take some time to be filled therefore it returns null,therefore I add a new Async task and implement the code in "indobackgroud" and "onpostexcute" the code also worked at android 9,however at android 10 it crashes and the layout reloaded from scratch and webview reload the website with losing the previous route ...any help please...thanks in advance...inform for more info.
edit1: some of the log that really does not help:
D/InputMethodManager: HSIFW - flag : 0 D/ViewRootImpl@3898813[MainActivity]: MSG_RESIZED: frame=(0,0,1920,1128) ci=(0,36,0,0) vi=(0,36,0,0) or=1 D/ViewRootImpl@3898813[MainActivity]: Relayout returned: old=(0,0,1920,1128) new=(0,0,1920,1128) req=(1920,1128)8 dur=24 res=0x5 s={false 0} ch=true D/ViewRootImpl@3898813[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0 1 D/InputMethodManager: prepareNavigationBarInfo() DecorView@104aa52[MainActivity] getNavigationBarColor() -855310 D/ViewRootImpl@3898813[MainActivity]: stopped(true) old=false D/ViewRootImpl@3898813[MainActivity]: Relayout returned: old=(0,0,1920,1128) new=(0,0,1920,1128) req=(1920,1128)8 dur=24 res=0x5 s={false 0} ch=false D/InputTransport: Input channel destroyed: 'ClientS', fd=64
the code after adding the Async task is like below:
new AsyncTask<Void, Void, Void>(){
Bitmap bitmap = null;
@Override
protected Void doInBackground(Void... voids) {
try {
bitmap = MediaStore.Images.Media.getBitmap(
context.getContentResolver(), imageUri);
ContentResolver contentResolver = context.getContentResolver();
contentResolver.delete(imageUri, null, null);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void unused) {
super.onPostExecute(unused);
if (bitmap != null) {
int width = Math.round((float) 0.2 * bitmap.getWidth());
int height = Math.round((float) 0.2 * bitmap.getHeight());
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, width,
height, false);
String base64Image = imageHandler.convertBase64(newBitmap);
base64Image = base64Image.replace("\n", "");
String s1 = "url('data:image/jpeg;base64," + base64Image + "') no-repeat 0% 0% / 100% 100% white";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webView.evaluateJavascript("javascript:" +
"$('[data-key=\"" + getElementId() + "\"] .Preview').css('background',\"" + s1 + "\");"
, null);
webView.evaluateJavascript("$('[data-key=\"" + getElementId() + "\"] input').val(\"" + base64Image + "\")", null);
}
} else {
Log.d("kiki", "fucking null bitmap");
// webView.evaluateJavascript("javascript:" +
// "$('[data-key=\"" + getElementId() + "\"] .Preview').css('background',\"\");"
// , null);
// webView.evaluateJavascript("$('[data-key=\"" + getElementId() + "\"] input').val(\"\")", null);
}
}
}.execute();