1.xml文件中添加
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/bannerWidgetId"
>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/myProgressBar"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_marginTop="-3dp"
android:max="100"
android:maxHeight="5dp"
android:minHeight="5dp"
android:progress="0"
/>
<WebView
android:id="@+id/webviewId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="3dp"
android:layerType="hardware"/>
</RelativeLayout>
2.java代码中获取webview和(ProgressBar)
myProgressBar = (ProgressBar) findViewById(R.id.myProgressBar);
webviewId = (WebView) findViewById(R.id.webviewId);
webviewId.setWebChromeClient(new MyWebChromeClient())
3.实现MyWebChromeClient类并且继承WebChromeClient
class MyWebChromeClient extends WebChromeClient {
//The undocumented magic method override
//Eclipse will swear at you if you try to put @Override here
// Android 3.0 版本支持打开文件
public void openFileChooser(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);
}
// 设置webview加载ProgressBar从开始到结束
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
myProgressBar.setVisibility(View.GONE);
} else {
if (myProgressBar.getVisibility() == View.GONE)
myProgressBar.setVisibility(View.VISIBLE);
myProgressBar.setProgress(newProgress);
}
super.onProgressChanged(view, newProgress);
}
//Android 4.0 版本支持打开文件
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
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);
// getActivity().startActivityForResult( Intent.createChooser( i, "File Chooser" ), FILECHOOSER_RESULTCODE );
}
//Android 5.0 版本支持打开文件
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
// fileChooserParams.getAcceptTypes();
mUploadMessages = filePathCallback;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
return true;
}
}
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/bannerWidgetId"
>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/myProgressBar"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_marginTop="-3dp"
android:max="100"
android:maxHeight="5dp"
android:minHeight="5dp"
android:progress="0"
/>
<WebView
android:id="@+id/webviewId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="3dp"
android:layerType="hardware"/>
</RelativeLayout>
2.java代码中获取webview和(ProgressBar)
myProgressBar = (ProgressBar) findViewById(R.id.myProgressBar);
webviewId = (WebView) findViewById(R.id.webviewId);
webviewId.setWebChromeClient(new MyWebChromeClient())
3.实现MyWebChromeClient类并且继承WebChromeClient
class MyWebChromeClient extends WebChromeClient {
//The undocumented magic method override
//Eclipse will swear at you if you try to put @Override here
// Android 3.0 版本支持打开文件
public void openFileChooser(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);
}
// 设置webview加载ProgressBar从开始到结束
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
myProgressBar.setVisibility(View.GONE);
} else {
if (myProgressBar.getVisibility() == View.GONE)
myProgressBar.setVisibility(View.VISIBLE);
myProgressBar.setProgress(newProgress);
}
super.onProgressChanged(view, newProgress);
}
//Android 4.0 版本支持打开文件
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
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);
// getActivity().startActivityForResult( Intent.createChooser( i, "File Chooser" ), FILECHOOSER_RESULTCODE );
}
//Android 5.0 版本支持打开文件
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
// fileChooserParams.getAcceptTypes();
mUploadMessages = filePathCallback;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
return true;
}
}