WebView中响应网页的“上传文件”需求

博客介绍了与Web端文件类型相关的操作步骤,包括提及web端要求的文件类型,如图片(image/*、image/jpeg等)、视频(video/*),还说明了设置类型以及反馈给web的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:

webView.getSettings().setJavaScriptEnabled(true);

第二步:

webView.setWebChromeClient(new WebChromeClient() {
//扩展浏览器上传文件
//3.0++版本
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
   openFileChooserImpl(uploadMsg, acceptType);
}

//3.0--版本
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
   openFileChooserImpl(uploadMsg, null);
}

public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
   openFileChooserImpl(uploadMsg, acceptType);
}

// For Android > 5.0
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> uploadMsg, FileChooserParams fileChooserParams) {
   String[] acceptTypes = fileChooserParams.getAcceptTypes();
   openFileChooserImplForAndroid5(uploadMsg, acceptTypes);
   return true;
}});

//acceptType即是web端要求的文件类型,一般有

图片:image/*
视频:video/*

图片:image/jpeg,image/gif,image/png

 

第三步:设置类型

private void openFileChooserImpl(ValueCallback<Uri> uploadMsg, String acceptType) {
   mUploadMessage = uploadMsg;
   Intent i = new Intent(Intent.ACTION_GET_CONTENT);
   i.addCategory(Intent.CATEGORY_OPENABLE);
   i.setType("*/*");
   if (acceptType != null) {
      i.putExtra(Intent.EXTRA_MIME_TYPES, new String[]{acceptType});
   }
   startActivityForResult(Intent.createChooser(i, "文件选择"), FILECHOOSER_RESULTCODE);
}

private void openFileChooserImplForAndroid5(ValueCallback<Uri[]> uploadMsg, String[] acceptTypes) {
   mUploadMessageForAndroid5 = uploadMsg;
   Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
   contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
   contentSelectionIntent.setType("*/*");
   if (acceptTypes != null) {
      contentSelectionIntent.putExtra(Intent.EXTRA_MIME_TYPES, acceptTypes);//支持多类型
   }
   Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
   chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
   chooserIntent.putExtra(Intent.EXTRA_TITLE, "文件选择");

   startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE_FOR_ANDROID_5);
}

第四步:反馈给web

@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);
      Logger.d("result:" + result);
      mUploadMessage = null;

   } else if (requestCode == FILECHOOSER_RESULTCODE_FOR_ANDROID_5) {
      if (null == mUploadMessageForAndroid5)
         return;
      Uri result = (intent == null || resultCode != RESULT_OK) ? null : intent.getData();
      if (result != null) {
         mUploadMessageForAndroid5.onReceiveValue(new Uri[]{result});
         Logger.d("result5:" + result);
      } else {
         mUploadMessageForAndroid5.onReceiveValue(new Uri[]{});
      }
      mUploadMessageForAndroid5 = null;
   }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值