一、介绍
二、实践代码
三、问题
一、介绍
当需要将H5页面接入android时,会遇到选择文件的问题。
如果是在web端,选择文件就会打开本地文件。而在安卓,需要先到安卓app,再到系统文件,选择完后再回到app.再回到H5页面,所以需要配置。
二、代码实践
1.先画界面
只需要一个webview, 加个id就好
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity2"> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
2.上java代码
public class MainActivity2 extends AppCompatActivity { private ValueCallback<Uri[]> fileUploadCallback; private String cameraPhotoPath; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); WebView webView = (WebView) findViewById(R.id.webview); // 允许解析js WebSettings settings=webView.getSettings(); settings.setJavaScriptEnabled(true); // 优先使用缓存 settings.setCacheMode(settings.LOAD_CACHE_ELSE_NETWORK); settings.setDomStorageEnabled(true); // 设置此属性,可任意比例缩放 settings.setUseWideViewPort(true); // 适应webview settings.setLoadWithOverviewMode(true); // 设置可以支持缩放 settings.setSupportZoom(true); // 设置出现缩放工具 settings.setBuiltInZoomControls(true); // 手势焦点 webView.requestFocusFromTouch(); // 打开内置浏览器 WebViewClient webViewClient=new WebViewClient(); webView.setWebViewClient(webViewClient); // 设置WebChromeClient,以处理文件选择请求 webView.setWebChromeClient(new MyWebChromeClient()); webView.loadUrl( "H5地址"); } // 自定义WebChromeClient以处理文件选择请求 private class MyWebChromeClient extends WebChromeClient { @Override public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { fileUploadCallback = filePathCallback; // 创建一个文件选择的Intent Intent intent = fileChooserParams.createIntent(); try { startActivityForResult(intent, 1); } catch (Exception e) { fileUploadCallback = null; return false; } return true; } } // 处理文件选择的结果 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1) { if (fileUploadCallback == null) { return; } Uri[] results = null; // 检查选择的文件是否为空 if (resultCode == RESULT_OK && data != null) { String dataString = data.getDataString(); if (dataString != null) { results = new Uri[]{Uri.parse(dataString)}; } } // 传递选择的文件给WebView fileUploadCallback.onReceiveValue(results); fileUploadCallback = null; } } }
3.权限,在AndroidManifest,文件增加文件读取权限
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" /> <!-- Required only if your app needs to access videos that other apps created. --> <uses-permission android:name="android.permission.READ_MEDIA_VIDEO" /> <!-- Required only if your app needs to access audio files that other apps created. --> <uses-permission android:name="android.permission.READ_MEDIA_AUDIO" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
三、问题
标签:文件,settings,安卓,fileUploadCallback,照片,webview,null,true,webView From: https://www.cnblogs.com/nobody-/p/17727943.html