MainActivity 签名页面
package com.sign.webview;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView _webview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_webview = findViewById(R.id.webview);
_webview.getSettings().setJavaScriptEnabled(true);
_webview.getSettings().setAllowUniversalAccessFromFileURLs(true);
_webview.setWebViewClient(new WebViewClient());
_webview.addJavascriptInterface(new Bridge(), "bridge");
_webview.loadUrl("file:///android_asset/index.html");
findViewById(R.id.btn_clear).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
_webview.loadUrl("javascript:clear()");
}
});
findViewById(R.id.btn_save).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
System.out.println("============ 开始保存 ============");
_webview.evaluateJavascript("save()", new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
System.out.println("============ 保存结束 ============" + Bridge.sb);
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("url", String.valueOf(Bridge.sb));
startActivity(intent);
}
});
}
}
});
}
}
MainActivity 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清空" />
<Button
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存" />
</LinearLayout>
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</LinearLayout>
保存后的字符串拼接转为bitmap图片显示
package com.sign.webview;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.widget.ImageView;
public class Main2Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ImageView imageView = findViewById(R.id.image);
String url = getIntent().getStringExtra("url");
imageView.setImageBitmap(stringtoBitmap(Stringbehind(url,"data:image/png;base64,")));//截取data:image/png;base64
}
public static String Stringbehind(String content, String string) {
//截取之后的字符
int length = string.length();
String str = content.substring(content.indexOf(string));
return str.substring(length);
}
public Bitmap stringtoBitmap(String string) {
//将字符串转换成Bitmap类型
Bitmap bitmap = null;
try {
byte[] bitmapArray;
bitmapArray = Base64.decode(string, Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
} catch (Exception e) {
e.printStackTrace();
Log.e("xhx", "base64转换图片异常");
}
return bitmap;
}
}
本地所需签名文件请在demo中下载 签名demo链接
|