作者:harekoc_303 | 来源:互联网 | 2023-09-08 12:29
第一步:获取网络权限,读写文件权限
<uses-feature android:name&#61;"android.hardware.camera"/>
<uses-feature android:name&#61;"android.hardware.camera.autofocus"/>
<uses-permission android:name&#61;"android.permission.INTERNET"/>
<uses-permission android:name&#61;"android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name&#61;"android.permission.READ_EXTERNAL_STORAGE"/>
第二步&#xff1a;加载本地写好的html文件&#xff08;定义好js提供给Android的调用方法jsCallAndroid()和安卓提供给js的调用接口upload()&#xff09;放在assets目录下。
<html>
<head>
<meta charset&#61;"UTF-8">
<title>title>
<style>
body{
text-align: center;
}
.one{
border: 3px solid blue;
}
.two{
border: 3px solid transparent;
}
style>
head>
<body>
<img id&#61;"photo" class&#61;"one" src&#61;"img/image3.png" width&#61;"300" height&#61;"300"/><br>
<button id&#61;"btn_jscallandroid" onclick&#61;"jsCallAndroid()">点击上传button><script type&#61;"text/Javascript">function jsCallAndroid(){var str&#61;"点击上传";objName.upload();}function setImag(path){document.getElementById("photo").src&#61;""&#43;path}function androidCallJsAddBorder(){document.getElementById("photo").className&#61;"one";}function androidCallJsRemoveBorder(){document.getElementById("photo").className&#61;"two";}
script>
body>
html>
第三步&#xff1a;Android部分代码&#xff1a;
public class MainActivity extends AppCompatActivity {private WebView mWebView;private Button mBtnAddBorder;private Button mBtnRemoveBorder;private SweetAlertDialog dialog;&#64;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initViews();initData();initListener();}private void initViews() {mWebView &#61; (WebView) findViewById(R.id.webview);mBtnAddBorder &#61; (Button) findViewById(R.id.btn_addborder);mBtnRemoveBorder &#61; (Button) findViewById(R.id.btn_removeborder);}private void initData() {WebSettings settings &#61; mWebView.getSettings();settings.setDefaultTextEncodingName("utf-8");settings.setJavascriptEnabled(true);settings.setBlockNetworkImage(false);mWebView.loadUrl("file:///android_asset/html/webview2js.html");mWebView.addJavascriptInterface(new Test(), "objName");}private void initListener() {mBtnRemoveBorder.setOnClickListener(new View.OnClickListener() {&#64;Overridepublic void onClick(View v) {mWebView.loadUrl("Javascript: androidCallJsRemoveBorder()");}});mWebView.setWebViewClient(new WebViewClient() {&#64;Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {view.loadUrl("file:///android_asset/html/webview2js.html");return true;}});mBtnAddBorder.setOnClickListener(new View.OnClickListener() {&#64;Overridepublic void onClick(View v) {mWebView.loadUrl("Javascript: androidCallJsAddBorder()");}});}class Test {&#64;JavascriptInterfacepublic void upload() {dialog &#61; new SweetAlertDialog(MainActivity.this);dialog.setCanceledOnTouchOutside(true);dialog.setTitleText("请选择获取头像方式&#xff01;").setCancelText("打开相册").setConfirmText("打开相机").setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {&#64;Overridepublic void onClick(SweetAlertDialog sweetAlertDialog) {openAlbum();}}).setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {&#64;Overridepublic void onClick(SweetAlertDialog sweetAlertDialog) {openCamera();}}).show();}}private void openAlbum() {Intent mIntent &#61; new Intent(Intent.ACTION_PICK);mIntent.setType("image/*");startActivityForResult(mIntent, 1);dialog.dismiss();}private void openCamera() {Intent intent &#61; new Intent(MediaStore.ACTION_IMAGE_CAPTURE);if (SDCardUtils.getInstance().ExistSDCard()) {File tempFile &#61; new File(Environment.getExternalStorageDirectory()&#43; "/androidtojs/" &#43; "photo.jpg");if (!tempFile.exists()) {tempFile.getParentFile().mkdirs();}Uri uri &#61; Uri.fromFile(tempFile);intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);}startActivityForResult(intent, 2);dialog.dismiss();}&#64;Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);switch (requestCode) {case 1: try {if (data !&#61; null) {Uri uri &#61; data.getData(); if (uri !&#61; null) {String path &#61; UriUtils.getInstance().getPath(MainActivity.this, uri);if (path &#61;&#61; null || path.equals("")) {Toast.makeText(MainActivity.this, "没找到图片,请拍照",Toast.LENGTH_SHORT).show();return;}File s &#61; new File(path);if (s.exists()) {Toast.makeText(MainActivity.this, path, Toast.LENGTH_SHORT).show();mWebView.loadUrl("Javascript:setImag(&#39;" &#43; path &#43; "&#39;)");} else {Toast.makeText(MainActivity.this, "没找到图片,请拍照",Toast.LENGTH_SHORT).show();}}} else {Toast.makeText(MainActivity.this, "没找到图片,请拍照", Toast.LENGTH_SHORT).show();}} catch (Exception e) {}break;case 2: if (resultCode &#61;&#61; RESULT_OK) {if (SDCardUtils.getInstance().ExistSDCard()) {File temp &#61; new File(Environment.getExternalStorageDirectory()&#43; "/androidtojs/" &#43; "photo.jpg");if (!temp.exists()) {temp.getParentFile().mkdirs();}String path &#61; temp.getPath();mWebView.loadUrl("Javascript:setImag(&#39;" &#43; path &#43; "&#39;)");} else {Toast.makeText(MainActivity.this, "未找到存储卡&#xff0c;无法存储照片&#xff01;", Toast.LENGTH_SHORT).show();}}break;}}
}
注意&#xff1a;该程序在安卓6.0下拍照上传之后显示不了的解决方法&#xff1a;
在build.gradle下把targetSdkVersion设置为22 &#xff0c;因为安卓6.0需要获取存取权限。