webview 不支援檔案上傳 <input type="file" />

來源:互聯網
上載者:User

標籤:jpeg   ==   cli   oid   ast   else   手機   []   安卓   

項目中遇到檔案上傳的需求,IOS直接是支援的,安卓端卻沒反應,有些機型甚至閃退。

找了很多原因,在網上找的辦法,實測可行,做個記錄,知識分享。

Html檔案上傳

<input type="file" class="uploadBtn" multiple="multiple" accept="image/jpeg">

 手機端WebView重寫方法

    // 1.設定WebChromeClient,重寫檔案上傳回調        mWebView.setWebChromeClient(new WebChromeClient() {                        // For Android < 3.0            public void openFileChooser(ValueCallback<Uri> valueCallback) {                mUploadMessage = valueCallback;                openImageChooserActivity();            }            // For Android  >= 3.0            public void openFileChooser(ValueCallback valueCallback, String acceptType) {                mUploadMessage = valueCallback;                openImageChooserActivity();            }            //For Android  >= 4.1            public void openFileChooser(ValueCallback<Uri> valueCallback, String acceptType, String capture) {                mUploadMessage = valueCallback;                openImageChooserActivity();            }            // For Android >= 5.0            @Override            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {                mUploadCallbackAboveL = filePathCallback;                openImageChooserActivity();                return true;            }        });        // 2.回調方法觸發本地選擇檔案            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"), FILECHOOSER_RESULTCODE);    }        // 3.選擇圖片後處理        @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;                }            }        }            // 4. 選擇內容回調到Html頁面        @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;        }    }

 

webview 不支援檔案上傳 <input type="file" />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.