第十二课webview

HTML的内容

<!DOCTYPE html>
<html>
   <head>
      <meta charset="UTF-8">
      <title></title>
      <script type="text/javascript">
         
         //调用安卓的方法window点标记点方法名
         
         function clickcamear(){
            window.android.getjs()
         }
         
         //安卓调用它来传递图片
         
         function showpic(path){
            document.getElementById("img").src=path;
         }
      </script>
   </head>
   <body>
      <img width="200" height="200" id="img" />
      <button onclick="clickcamear()">点击按钮拍照</button>
   </body>
</html>

添加权限并且动态申请


public class MainActivity extends AppCompatActivity {

    private WebView webView;
    private Uri uri;

    @SuppressLint("JavascriptInterface")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView=findViewById(R.id.webview);

        webView.loadUrl("file:///android_asset/camear.html");

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAllowFileAccess(true);

        webView.addJavascriptInterface(this,"android");
//        getjs();
        if (Build.VERSION.SDK_INT>Build.VERSION_CODES.M){
            requestPermissions(new String[]{Manifest.permission.CAMERA,
                    Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},100);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode==100&&grantResults[0]== PackageManager.PERMISSION_GRANTED){

        }
    }

//    @SuppressLint("JavascriptInterface")
    @JavascriptInterface
    public void getjs() {
        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        uri= FileProvider.getUriForFile(this,"com.wanglu",new File("/sdcard/DCIM/Camera/ababba.jpg"));
        intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
        startActivityForResult(intent,101);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==101&&resultCode== Activity.RESULT_OK){
            webView.loadUrl("JavaScript:showpic('/sdcard/DCIM/Camera/ababba.jpg')");
        }
    }
}


js掉方法要加标记

@JavascriptInterface

加载外部网页

 webView.loadUrl("http://www.baidu.com");
         webView.setWebViewClient(new WebViewClient(){//将网页加载到自己的webview中
             @Override
             public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                 view.loadUrl("http://www.baidu.com");
                 return true;
             }
         });
``
添加进度条

final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage(“正在加载”);
progressDialog.show();
//TODO 2:设置加载进度,当没有加载完毕 显示进度条对话框
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
Log.d(“ytx”, "onProgressChanged: "+newProgress);
if(newProgress==100){
progressDialog.dismiss();
}
}
});

[更详细](https://blog.csdn.net/qq_34178710/article/details/99689359)


上一篇:MQ的基本概念


下一篇:Redis 基于 RedisShake 集群迁移