真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Android解決WebView無法上傳文件的問題

Android 解決WebView無法上傳文件的問題

渭源網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),渭源網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為渭源1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的渭源做網(wǎng)站的公司定做!

Android原生的WebView并不支持上傳文件,需要我們自己實(shí)現(xiàn)相應(yīng)的方法。于是我把工作中的相關(guān)代碼記錄下來。下次直接拿來用就行了。一點(diǎn)一滴都是經(jīng)驗(yàn)。

1。需要定義三個(gè)變量

 private ValueCallback uploadMessageAboveL;
  private final static int FILE_CHOOSER_RESULT_CODE = 10000;
  private ValueCallback uploadMessage;

2.重寫Activity相關(guān)方法

  private void openImageChooserActivity() {
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.addCategory(Intent.CATEGORY_OPENABLE);
    i.setType("image/*");
    startActivityForResult(Intent.createChooser(i, "Image Chooser"), FILE_CHOOSER_RESULT_CODE);
  }


  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == FILE_CHOOSER_RESULT_CODE) {
      if (null == uploadMessage && null == uploadMessageAboveL) return;
      Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
      if (uploadMessageAboveL != null) {
        onActivityResultAboveL(requestCode, resultCode, data);
      } else if (uploadMessage != null) {
        uploadMessage.onReceiveValue(result);
        uploadMessage = null;
      }
    }
  }


  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  private void onActivityResultAboveL(int requestCode, int resultCode, Intent intent) {
    if (requestCode != FILE_CHOOSER_RESULT_CODE || uploadMessageAboveL == null)
      return;
    Uri[] results = null;
    if (resultCode == Activity.RESULT_OK) {
      if (intent != null) {
        String dataString = intent.getDataString();
        ClipData clipData = intent.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)};
      }
    }
    uploadMessageAboveL.onReceiveValue(results);
    uploadMessageAboveL = null;
  }

3.重寫WebChromeClient中的相關(guān)方法

 // For Android < 3.0
    public void openFileChooser(ValueCallback valueCallback) {
      uploadMessage = valueCallback;
      openImageChooserActivity();
    }

    // For Android >= 3.0
    public void openFileChooser(ValueCallback valueCallback, String acceptType) {
      uploadMessage = valueCallback;
      openImageChooserActivity();
    }

    //For Android >= 4.1
    public void openFileChooser(ValueCallback valueCallback, String acceptType, String capture) {
      uploadMessage = valueCallback;
      openImageChooserActivity();
    }

    // For Android >= 5.0
    @Override
    public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
      uploadMessageAboveL = filePathCallback;
      openImageChooserActivity();
      return true;
    }

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!


分享名稱:Android解決WebView無法上傳文件的問題
本文網(wǎng)址:http://weahome.cn/article/jpgieg.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部