Android用webView加载h5的页面,h5获取本地的图库中的图片
2017-06-19 15:02
639 查看
效果图:
![](https://img-blog.csdn.net/20170619144843566?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWh5MTIzNDU2cQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20170619150418666?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWh5MTIzNDU2cQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
代码:
2.布局文件:activity_web_view.xml
添加权限:AndroidMainifet.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testandroid.webview">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".WebViewActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
demo下载:http://download.csdn.net/detail/yhy123456q/9874464点击打开链接
代码:
package com.testandroid.webview; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.KeyEvent; import android.view.View; import android.webkit.JsResult; import android.webkit.ValueCallback; import android.webkit.WebBackForwardList; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; public class WebViewActivity extends AppCompatActivity { private final String TAG = WebViewActivity.class.getSimpleName(); private Button button; private WebView webView; // private String recgPic = "http://m.shitu.chinaso.com/mx/index.html"; private String recgPic = "http://t.2858.com/party-api/m/news/enter/detail/178.html?token=7fcec1183b8587f1dc51dcc0286bb450&fontSize=1.0"; public final static int FILECHOOSER_RESULTCODE = 1; public final static int FILECHOOSER_RESULTCODE_FOR_ANDROID_5 = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_web_view); button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); initTestWebView(); } private void initTestWebView() { webView = (WebView) findViewById(R.id.tempWebView); WebSettings settings = webView.getSettings(); settings.setJavaScriptEnabled(true); webView.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext()); builder.setTitle("xxx提示").setMessage(message).setPositiveButton("确定", null); builder.setCancelable(false); builder.setIcon(R.mipmap.ic_launcher); AlertDialog dialog = builder.create(); dialog.show(); result.confirm(); return true; } //扩展浏览器上传文件 //3.0++版本 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { openFileChooserImpl(uploadMsg); } //3.0--版本 public void openFileChooser(ValueCallback<Uri> uploadMsg) { openFileChooserImpl(uploadMsg); } public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { openFileChooserImpl(uploadMsg); } @Override public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { onenFileChooseImpleForAndroid(filePathCallback); return true; } }); webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); webView.loadUrl(recgPic); } public ValueCallback<Uri> mUploadMessage; public void openFileChooserImpl(ValueCallback<Uri> uploadMsg) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); } public ValueCallback<Uri[]> mUploadMessageForAndroid5; public void onenFileChooseImpleForAndroid(ValueCallback<Uri[]> filePathCallback) { mUploadMessageForAndroid5 = filePathCallback; Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); contentSelectionIntent.setType("image/*"); Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE_FOR_ANDROID_5); } @Override protected void onActivityResult(int requestCode, int resultCode,Intent intent) { if (requestCode == FILECHOOSER_RESULTCODE) { if (null == mUploadMessage) return; Uri result = intent == null || resultCode != RESULT_OK ? null: intent.getData(); mUploadMessage.onReceiveValue(result); mUploadMessage = null; } else if (requestCode == FILECHOOSER_RESULTCODE_FOR_ANDROID_5){ if (null == mUploadMessageForAndroid5) return; Uri result = (intent == null || resultCode != RESULT_OK) ? null: intent.getData(); if (result != null) { mUploadMessageForAndroid5.onReceiveValue(new Uri[]{result}); } else { mUploadMessageForAndroid5.onReceiveValue(new Uri[]{}); } mUploadMessageForAndroid5 = null; } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (webView.canGoBack() && event.getKeyCode() == KeyEvent.KEYCODE_BACK) { //获取历史列表 WebBackForwardList mWebBackForwardList = webView .copyBackForwardList(); //判断当前历史列表是否最顶端,其实canGoBack已经判断过 if (mWebBackForwardList.getCurrentIndex() > 0) { webView.goBack(); return true; } } return super.onKeyDown(keyCode, event); b38a } }
2.布局文件:activity_web_view.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <WebView android:id="@+id/tempWebView" android:layout_width="match_parent" android:layout_height="match_parent"></WebView> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
添加权限:AndroidMainifet.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testandroid.webview">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".WebViewActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
demo下载:http://download.csdn.net/detail/yhy123456q/9874464点击打开链接
相关文章推荐
- H5页面打开android本地照册,当webview页面被系统回收的情况下,如何处理图片
- Android使用WebView加载本地图片终极大杀招
- android WebView 控件加载本地sdcard中html文件图片的问题
- Android webView 加载网页时,使用本地图片替换网页内的图片
- Android webview加载Html页面,传参到Html并获取返回值
- android WebView加载html 并 引用本地资源(图片、字体库)
- Android - webview拦截css,js,图片后加载本地外部存储的文件(shouldOverrideUrlLoading)
- android webview 网络页面从本地(assets)加载js库
- android用webview加载H5页面出现点击事件失效的问题解决
- android使用webview加载H5页面
- android webview加载H5页面丢失cookie信息的解决办法
- Webview之H5页面调用android的图库及文件管理
- Android webView 加载网页时,使用本地图片替换网页内的图片
- Android开发——webview获取H5的Cookie并保存在本地
- Android开发 WebView加载从本地加载图片
- Webview之H5页面调用android的图库及文件管理
- Android中使用WebView加载H5页面的方法
- Android webview加载本地图片
- android webview 加载H5页面,设置字体不随系统设置变化
- Android中Webviw加载H5页面调用本地相机拍照并显示在H5页面上