html能调用 webview调用相册,WebView如何调用系统的相机和相册上传文件

private ValueCallback mUploadMessage;// 表单的数据信息

private ValueCallback mUploadCallbackAboveL;

private final static int FILECHOOSER_RESULTCODE = 1;// 表单的结果回调

private Uri imageUri;

/**

* web视图

*/

mWebView.setWebChromeClient(new WebChromeClient() {

//设置网页加载的进度条

@Override

public void onProgressChanged(WebView view, int newProgress) {

super.onProgressChanged(view, newProgress);

if (newProgress == 100) {

mProgressBar.setVisibility(View.GONE);

} else {

mProgressBar.setVisibility(View.VISIBLE);

mProgressBar.setProgress(newProgress);

}

}

//android>5.0调用这个方法

@Override

public boolean onShowFileChooser(WebView webView,

ValueCallback filePathCallback,

FileChooserParams fileChooserParams) {

mUploadCallbackAboveL=filePathCallback;

take();

return true;

}

public void openFileChooser(ValueCallback uploadMsg) {

mUploadMessage=uploadMsg;

take();

}

public void openFileChooser(ValueCallback uploadMsg,String acceptType) {

mUploadMessage=uploadMsg;

take();

}

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

mUploadMessage=uploadMsg;

take();

}

});

if (!TextUtils.isEmpty(mWebUrl)) {

mBrowserLayout.loadUrl(mWebUrl);

} else {

Toast.makeText(this, "获取URL地址失败", Toast.LENGTH_SHORT).show();

}

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if(requestCode==FILECHOOSER_RESULTCODE)

{

if (null == mUploadMessage && null == mUploadCallbackAboveL) return;

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

if (mUploadCallbackAboveL != null) {

onActivityResultAboveL(requestCode, resultCode, data);

}

else if (mUploadMessage != null) {

Log.e("result",result+"");

if(result==null){

// mUploadMessage.onReceiveValue(imageUri);

mUploadMessage.onReceiveValue(imageUri);

mUploadMessage = null;

Log.e("imageUri",imageUri+"");

}else {

mUploadMessage.onReceiveValue(result);

mUploadMessage = null;

}

}

}

}

@SuppressWarnings("null")

@TargetApi(Build.VERSION_CODES.LOLLIPOP)

private void onActivityResultAboveL(int requestCode, int resultCode, Intent data) {

if (requestCode != FILECHOOSER_RESULTCODE

|| mUploadCallbackAboveL == null) {

return;

}

Uri[] results = null;

if (resultCode == Activity.RESULT_OK) {

if (data == null) {

results = new Uri[]{imageUri};

} else {

String dataString = data.getDataString();

ClipData clipData = data.getClipData();

if (clipData != null) {

results = new Uri[clipData.getItemCount()];

for (int i = 0; i < clipData.getItemCount(); i++) {

ClipData.Item item = clipData.getItemAt(i);

results[i] = item.getUri();

}

}

if (dataString != null)

results = new Uri[]{Uri.parse(dataString)};

}

}

if(results!=null){

mUploadCallbackAboveL.onReceiveValue(results);

mUploadCallbackAboveL = null;

}else{

results = new Uri[]{imageUri};

mUploadCallbackAboveL.onReceiveValue(results);

mUploadCallbackAboveL = null;

}

return;

}

private void take(){

File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp");

// Create the storage directory if it does not exist

if (! imageStorageDir.exists()){

imageStorageDir.mkdirs();

}

File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");

imageUri = Uri.fromFile(file);

final List cameraIntents = new ArrayList();

final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

final PackageManager packageManager = getPackageManager();

final List listCam = packageManager.queryIntentActivities(captureIntent, 0);

for(ResolveInfo res : listCam) {

final String packageName = res.activityInfo.packageName;

final Intent i = new Intent(captureIntent);

i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));

i.setPackage(packageName);

i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

cameraIntents.add(i);

}

Intent i = new Intent(Intent.ACTION_GET_CONTENT);

i.addCategory(Intent.CATEGORY_OPENABLE);

i.setType("image/*");

Intent chooserIntent = Intent.createChooser(i,"Image Chooser");

chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));

ServiceWebActivity.this.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值