Android内嵌H5页面调用手机图片操作

我们在APP中有一个功能是引导商家入驻我们平台,商家入驻就需要填写一些企业信息和上传营业执照或宣传照片等。为了考虑到方便及兼容性问题,我们产品就考虑用H5开发这个功能,然后内嵌到APP中,刚开始我们APP端开发的满心欢喜,因为用H5开发,我们直接放在WebView就可以了,方便省事,但后来才知道我们是空欢喜一场,因为遇到了令人很头痛的兼容性问题。页面很简单,如下:

红色箭头就是这次兼容性问题的根本。因为这个页面纯H5开发的,添加图片功能在APP上本来应该没问题,在ios上确实没问题,但是在我们Android上点击添加图片居然没有任何反应。后来百度,必应等搜索,加上请同事帮忙,才知道问题出在哪里。

    我们的实现原理还是一样的,也是用WebView以load的方式加载h5页面,不过要实现H5在APP上操作图片,我们就需要重写WebChromeClient中的openFileChooser方法才行,顺便百度了下WebView中 WebViewClient与WebChromeClient的区别

WebViewClient主要帮助WebView处理各种通知、请求事件的,比如:

onLoadResource
onPageStart
onPageFinish
onReceiveError
onReceivedHttpAuthRequest

WebChromeClient主要辅助WebView处理Javascript的对话框、网站图标、网站title、加载进度等比如

onCloseWindow(关闭WebView)
onCreateWindow()
onJsAlert (WebView上alert无效,需要定制WebChromeClient处理弹出)
onJsPrompt
onJsConfirm
onProgressChanged
onReceivedIcon
onReceivedTitle

看上去他们有很多不同,实际使用的话,如果你的WebView只是用来处理一些html的页面内容,只用WebViewClient就行了,如果需要更丰富的处理效果,比如JS、进度条等,就要用到WebChromeClient。因为我们只需要要处理Html中图片上传效果,所以我们用到了WebChromeClient。利益于这两篇文章的帮助 :http://blog.csdn.net/woshinia/article/details/19030437http://magiclen.org/android-filechooser/,后来通过一些处理,基本实现了图片上传的功能 ,最终我们的代码如下:

mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String mTitle) {
        super.onReceivedTitle(view, mTitle);

                                   //这里可以用来处理我们的页面标题

}
@Override
public void onProgressChanged(WebView view, int newProgress) {
        //处理网页面加载进度
}

                
// For Android  < 3.0
public void openFileChooser(ValueCallback<Uri> filePathCallback) {
          mFilePathCallback4 = filePathCallback;
          IntentUtils.jumpToSelectPics(aty, false, 1, MultiImageSelectorActivity.MODE_SINGLE);
}

// For Android  > 4.1.1
public void openFileChooser(ValueCallback filePathCallback, String acceptType) {
         mFilePathCallback4 = filePathCallback;
         IntentUtils.jumpToSelectPics(aty, false, 1, MultiImageSelectorActivity.MODE_MULTI);
}

// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> filePathCallback, String acceptType, String capture) {
         mFilePathCallback4 = filePathCallback;
         IntentUtils.jumpToSelectPics(aty, false, 1, MultiImageSelectorActivity.MODE_MULTI);
}

public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,
         WebChromeClient.FileChooserParams fileChooserParams) {
         mFilePathCallback5 = filePathCallback;
         IntentUtils.jumpToSelectPics(aty, false, 1, MultiImageSelectorActivity.MODE_MULTI);
         return true;
}
});

我们这里选择好图片后,还要进行处理:

@Override

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
         if (requestCode == IntentUtils.REQUEST_IMAGE) {
                  if (mFilePathCallback4 != null) {
                           if (null != intent) {
                                    ArrayList<String> resultList = intent.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
                                    if (resultList.size() > 0) {
                                             String path = MediaUtility.getPath(BaseWebViewAct.this,
                                             Uri.parse("file://" + resultList.get(0)));//
                                             Uri uri = Uri.fromFile(new File(path));
                                             mFilePathCallback4.onReceiveValue(uri);
                                    }
                           }else {
                                    mFilePathCallback4.onReceiveValue(null);
                           }
                  }
                           if (mFilePathCallback5 != null) {
                                    if (null != intent) {
                                             ArrayList<String> resultList = intent.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
                                             if (resultList.size() > 0) {
                                                      String path = MediaUtility.getPath(BaseWebViewAct.this,
                                                      Uri.parse("file://" + resultList.get(0)));//
                                                      Uri uri = Uri.fromFile(new File(path));
                                                      mFilePathCallback5.onReceiveValue(new Uri[]{uri});
                                             }
                                    }else {
                                             mFilePathCallback5.onReceiveValue(null);
                                    }
                           }
                           mFilePathCallback4 = null;
                           mFilePathCallback5 = null;
                  }
}
Android应用开发中,内嵌HTML5页面通常是指将Web内容作为Activity的一部分展示给用户。这种做法利用了WebView组件,它是一个可以显示网页内容的视图。以下是创建和使用H5页面的基本步骤: 1. **引入依赖**:在AndroidManifest.xml文件中添加权限,并确保已导入`<uses-permission android:name="android.permission.INTERNET" />`以允许访问网络。 ```xml <uses-permission android:name="android.permission.INTERNET" /> <activity ... <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="http" /> <data android:host="yourdomain.com" /> </intent-filter> </activity> ``` 2. **在布局文件中添加WebView**:在XML布局文件中添加WebView控件,例如放在`<FrameLayout>`。 ```xml <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal" /> ``` 3. **初始化并加载H5**:在Activity中获取WebView实例,设置其属性,然后加载URL。 ```java WebView webView = findViewById(R.id.webView); webView.loadUrl("https://www.example.com/index.html"); webView.setWebViewClient(new WebViewClient()); webView.getSettings().setJavaScriptEnabled(true); // 允许运行JavaScript ``` 4. **处理交互**:如果需要,你可以通过监听WebView的各种事件(如 onPageFinished()、onLoadResource() 等),以及JavaScript Bridge技术与H5页面进行通信。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值