WebView加载页面 页面上上传图片

private Object getHtmlObject() {
		Object insertObj = new Object() {

			// public void shlnk(final String site_url) {
			public void sharelink(final String site_url,
					final String image_url, final String site_name) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
					}
				});
			}

		};

		return insertObj;
	}
 // 获得WebView组件 
WebView webView = (WebView) findViewById(R.id.webview); 
webView.requestFocus();
 // 触摸焦点起作用 
// 设置页面是否可以用到
js webView.getSettings().setJavaScriptEnabled(true); 
// 设置WebView的一些缩放功能点 // 滚动条风格 
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
// 取消滚动条 webView.setHorizontalScrollBarEnabled(false); 
// webView.getSettings().setSupportZoom(true); 
// webView.getSettings().setBuiltInZoomControls(true);
 webView.setHorizontalScrollbarOverlay(true); // 设置默认为utf-8 
// webView.getSettings().setDefaultTextEncodingName("gbk"); 
webView.getSettings().setDefaultTextEncodingName("utf-8"); 
// 在同种分辨率的情况下,屏幕密度不一样的情况下,自动适配页面 
// DisplayMetrics dm = getResources().getDisplayMetrics(); 
// int scale = dm.densityDpi; 
// if (scale == 240) { 
// // webView.getSettings().setDefaultZoom(ZoomDensity.FAR); 
// } else if (scale == 160) { 
// webView.getSettings().setDefaultZoom(ZoomDensity.MEDIUM); 
// } else { // webView.getSettings().setDefaultZoom(ZoomDensity.CLOSE); // }

 

// 判断加载的页面URL地址是否正确
		if (URLUtil.isNetworkUrl(Urls.URL) == true) {
			// webView.addJavascriptInterface(getHtmlObject(), "jsObj");
			// webView.loadUrl("http://172.**.**.**/js.html");
			webView.addJavascriptInterface(getHtmlObject(), "jsObj");
			// webView.loadUrl("http://172.**.**.**//index.html");
			// 加载

			webView.loadUrl(Urls.URL);
                        //webView.loadUrl("javascript: add('"
      + bn+ "')");
			// 设置web视图客户端
			webView.setWebViewClient(new WebViewClient() {
				@Override
				public boolean shouldOverrideUrlLoading(WebView view, String url) {
					// 设置点击网页里面的链接还是在当前的webview里跳转
					view.loadUrl(url);
					return true;
				}

				@Override
				public void onPageStarted(WebView view, String url,
						Bitmap favicon) {
					// TODO Auto-generated method stub
					super.onPageStarted(view, url, favicon);

										rl_load_progress.setVisibility(View.VISIBLE);
											webView.getSettings().setSupportZoom(true);
						webView.getSettings().setBuiltInZoomControls(true);
						// webView.setInitialScale(70);
									}

				// @Override
				// public void onReceivedSslError(WebView view,
				// SslErrorHandler handler, android.net.http.SslError error) {
				// // 设置webview处理https请求
				// handler.proceed();
				// }

				// @Override
				// public void onReceivedError(WebView view, int errorCode,
				// String description, String failingUrl) {
				// super.onReceivedError(view, errorCode, description,
				// failingUrl);
				// // Toast.makeText(StartActivity.this,
				// // errorCode + " " + description, Toast.LENGTH_SHORT)
				// // .show();
				// }

				@Override
				public void onPageFinished(WebView view, String url) {
					// TODO Auto-generated method stub
					super.onPageFinished(view, url);

											rl_load_progress.setVisibility(View.GONE);
				}
			});
			WebChromeClient chromeClient = new WebChromeClientImpl();
			webView.setWebChromeClient(chromeClient);
		}

 

final public static int FILE_SELECTED = 4;
	public static ValueCallback<Uri> mUploadMessage;
private class WebChromeClientImpl extends WebChromeClient {

		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			// pb.setProgress(newProgress);
			// if (newProgress == 100) {
			// pb.setVisibility(View.GONE);
			// }
			super.onProgressChanged(view, newProgress);
		}

		// 扩展支持alert事件
		@Override
		public boolean onJsAlert(WebView view, String url, String message,
				JsResult result) {
			Builder builder = new Builder(view.getContext());
			builder.setTitle("商机通提示").setMessage(message)
					.setPositiveButton("确定", null);
			builder.setCancelable(false);
			builder.setIcon(R.drawable.ic_launcher);
			AlertDialog dialog = builder.create();
			dialog.show();
			result.confirm();
			return true;
		}

		// 扩展浏览器上传文件
		// 3.0++版本
		public void openFileChooser(ValueCallback<Uri> uploadMsg,
				String acceptType) {
			mUploadMessage = uploadMsg;
			Intent i = new Intent(Intent.ACTION_GET_CONTENT);
			i.addCategory(Intent.CATEGORY_OPENABLE);
			i.setType("image/*");
			StartActivity.this.startActivityForResult(
					Intent.createChooser(i, "File Chooser"), FILE_SELECTED);

		}

		// 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/*");
			StartActivity.this.startActivityForResult(
					Intent.createChooser(i, "file Browser"), FILE_SELECTED);
		}

		// For Android 4.1
		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/*");
			StartActivity.this.startActivityForResult(
					Intent.createChooser(i, "File Chooser"), FILE_SELECTED);

		}

	}

	protected class myWebChromeClient extends WebChromeClient {
		// For Android 3.0+
		public void openFileChooser(ValueCallback<Uri> uploadMsg,
				String acceptType) {
			mUploadMessage = uploadMsg;
			Intent i = new Intent(Intent.ACTION_GET_CONTENT);
			i.addCategory(Intent.CATEGORY_OPENABLE);
			i.setType("*/*");
			StartActivity.this.startActivityForResult(
					Intent.createChooser(i, "File Chooser"), FILE_SELECTED);
		}

		// For 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("*/*");
			StartActivity.this.startActivityForResult(
					Intent.createChooser(i, "File Chooser"), FILE_SELECTED);
		}
	}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
		System.out.println("sssssss");
if (requestCode == FILE_SELECTED) {
			Log.e("TAG4", "onActivityResult");
			if (null == mUploadMessage)
				return;
			Uri result = data == null || resultCode != RESULT_OK ? null : data
					.getData();

			/*
			 * String[] proj = { MediaStore.Images.Media.DATA };
			 * 
			 * Cursor actualimagecursor =
			 * managedQuery(result,proj,null,null,null);
			 * 
			 * int actual_image_column_index =
			 * actualimagecursor.getColumnIndexOrThrow
			 * (MediaStore.Images.Media.DATA);
			 * 
			 * actualimagecursor.moveToFirst();
			 * 
			 * String img_path =
			 * actualimagecursor.getString(actual_image_column_index);
			 * 
			 * File file = new File(img_path);
			 */
			mUploadMessage.onReceiveValue(result);
			mUploadMessage = null;
			Log.e("file", "file");

		}
	}

 

你可能感兴趣的:(JavaScript,android,上传图片,webView)