android webview 文件选择,Android – 在WebView中选择“文件”按钮

为选择文件设置WebChromeClient

import android.app.AlertDialog;

import android.content.Context;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.webkit.JavascriptInterface;

import android.webkit.ValueCallback;

import android.webkit.WebChromeClient;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.ProgressBar;

import android.widget.Toast;

import org.apache.http.util.EncodingUtils;

public class BrowserScreen extends Activity {

private WebView webView;

private String url = "url";

private ValueCallback mUploadMessage;

private final static int FILECHOOSER_RESULTCODE = 1;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.browser_activity);

initFields();

setListeners();

}

public void initFields() {

// TODO Auto-generated method stub

webView = (WebView) findViewById(R.id.webView1);

webView.getSettings().setJavaScriptEnabled(true);

webView.getSettings().setBuiltInZoomControls(true);

webView.getSettings().setAllowFileAccess(true);

}

public void setListeners() {

// TODO Auto-generated method stub

webView.setWebViewClient(new WebViewClient() {

public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

webView.loadUrl("about:blank");

view.clearHistory();

}

});

webView.setWebChromeClient(new WebChromeClient() {

public void onProgressChanged(WebView view, int progress) {

}

//The undocumented magic method override

//Eclipse will swear at you if you try to put @Override here

// For Android 3.0+

public void openFileChooser(ValueCallback 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);

}

// For Android 3.0+

public void openFileChooser(ValueCallback uploadMsg, String acceptType) {

mUploadMessage = uploadMsg;

Intent i = new Intent(Intent.ACTION_GET_CONTENT);

i.addCategory(Intent.CATEGORY_OPENABLE);

i.setType("*/*");

startActivityForResult(

Intent.createChooser(i, "File Browser"),

FILECHOOSER_RESULTCODE);

}

//For Android 4.1

public void openFileChooser(ValueCallback 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);

}

});

webView.loadUrl(url);

final MyJavaScriptInterface myJavaScriptInterface

= new MyJavaScriptInterface(this);

webView.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");

}

@Override

public void onBackPressed() {

// TODO Auto-generated method stub

if (webView.canGoBack() == true) {

webView.goBack();

} else {

super.onBackPressed();

}

}

public class MyJavaScriptInterface {

Context mContext;

MyJavaScriptInterface(Context c) {

mContext = c;

}

@JavascriptInterface

public void showToast(String toast) {

Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();

// webView.loadUrl("javascript:document.getElementById(\"Button3\").innerHTML = \"bye\";");

}

@JavascriptInterface

public void openAndroidDialog() {

AlertDialog.Builder myDialog

= new AlertDialog.Builder(BrowserScreen.this);

myDialog.setTitle("DANGER!");

myDialog.setMessage("You can do what you want!");

myDialog.setPositiveButton("ON", null);

myDialog.show();

}

}

@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;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值