android使用webview上传文件,android使用webview上传文件(支持相册和拍照)

mWebView.setWebChromeClient(new TestWebChromeClient(new WebChromeClient()) {@Override public void openFileChooser(ValueCallback uploadFile) { //

===文一==================================================================mWebView.setWebChromeClient(new TestWebChromeClient(new WebChromeClient()) {@Override

public void openFileChooser(ValueCallback uploadFile) {

// TODO Auto-generated method stub

if (mUploadMessage != null)

return;

mUploadMessage = uploadFile;

Intent i = new Intent(Intent.ACTION_GET_CONTENT);

i.addCategory(Intent.CATEGORY_OPENABLE);

i.setType("*/*");

TestActivity.this.startActivityForResult(Intent.createChooser(

i, getString(R.string.choose_upload)), FILE_SELECTED);

}

});

===文二===================================================================

网页上的button定义

方法一,也是网上能搜到最多的解决方案

参考网址:

@Overrideprotected void onActivityResult(int requestCode, intresultCode, Intent data) {if(requestCode==FILECHOOSER_RESULTCODE) {if (null == mUploadMessage) return;

Uri result= data == null || resultCode != RESULT_OK ? null: data.getData();

mUploadMessage.onReceiveValue(result);

mUploadMessage= null;

}

在WebChromeClient的设定中加入如下代码:

setting.setWebChromeClient(newWebChromeClient() {openFileChooser(ValueCallbackuploadMsg, String acceptType) {if (mUploadMessage != null) return;

mUploadMessage=uploadMsg;

Intent i= newIntent(Intent.ACTION_GET_CONTENT);

i.addCategory(Intent.CATEGORY_OPENABLE);

i.setType("*/*");

startActivityForResult( Intent.createChooser( i,"File Chooser"), BrowserActivity.FILECHOOSER_RESULTCODE );

}openFileChooser(ValueCallbackuploadMsg) {

openFileChooser( uploadMsg,"");

}

});

// For Android > 4.1.1

public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {

openFileChooser(uploadMsg, acceptType);

}

openFileChooser为隐藏方法。

这样可以实现打开目录上传文件。不过在3.0以后的版本中,如果同样的网页在android自带的浏览器打开会发现,

还有照相,录音的功能,然后直接将生成的图片或音频文件上传,这样就要使用下面的方法。

方法二,参考自com.android.browser的源码程式。

@Overrideprotected void onActivityResult(int requestCode, intresultCode, Intent data) {if(requestCode==FILECHOOSER_RESULTCODE) {if (null == mUploadMessage) return;

Uri result= data == null || resultCode != RESULT_OK ? null: data.getData();if (result == null && data == null && resultCode ==Activity.RESULT_OK) {

File cameraFile= newFile(mCameraFilePath);if(cameraFile.exists()) {

result=Uri.fromFile(cameraFile);//Broadcast to the media scanner that we have a new photo//so it will be added into the gallery for the user.

sendBroadcast(newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, result));

}

}

mUploadMessage.onReceiveValue(result);

mUploadMessage= null;

}

在WebChromeClient的设定中加入如下代码:

setting.setWebChromeClient(newWebChromeClient() {openFileChooser(ValueCallbackuploadMsg, String acceptType) {if (mUploadMessage != null) return;

mUploadMessage=uploadMsg;

startActivityForResult(createDefaultOpenableIntent(),

BrowserActivity.FILECHOOSER_RESULTCODE);

}

}openFileChooser(ValueCallbackuploadMsg) {

openFileChooser( uploadMsg,"");

}

});

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值