webView

package com.example.webview;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Window;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

private WebView mWeView;
private Uri uri;

@SuppressLint("JavascriptInterface")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){
        requestPermissions(new String[]{
                Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE
        },110);
    }

    initView();
    WebSettings settings = mWeView.getSettings();
    mWeView.setWebViewClient(new WebViewClient());
    mWeView.setWebChromeClient(new WebChromeClient());
    settings.setUseWideViewPort(true);//设置此属性,可任意比例缩放
    settings.setLoadWithOverviewMode(true);
    settings.setJavaScriptEnabled(true);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);
    settings.setSupportZoom(true);
    settings.setAllowFileAccess(true);
    mWeView.addJavascriptInterface(this,"spark");
    mWeView.loadUrl("file:///android_asset/index.html");

// mWeView.loadUrl(“https://blog.csdn.net/qq_34178710/article/details/99689359#2js_104”);

}
@JavascriptInterface  //接口
public void paizhao(){
    Intent intent = new Intent();
    intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
    uri= FileProvider.getUriForFile(this, "com.example.webview.fileProvider", new File("/sdcard/DCIM/html.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){
        //安卓调用js代码
        mWeView.loadUrl("JavaScript:image_arg('/sdcard/DCIM/html.jpg')");

    }
}

private void initView() {
    mWeView = findViewById(R.id.weView);
}

}

你可能感兴趣的:(webView)