溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

webview怎么適配html5實(shí)現(xiàn)照片視頻上傳

發(fā)布時(shí)間:2022-03-01 09:51:25 來(lái)源:億速云 閱讀:198 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了webview怎么適配html5實(shí)現(xiàn)照片視頻上傳的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇webview怎么適配html5實(shí)現(xiàn)照片視頻上傳文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

一、需要實(shí)現(xiàn)的功能:

用H5實(shí)現(xiàn)的App中需要在H5獲取手機(jī)中的照片或者視頻文件上傳到服務(wù)器。

 webview怎么適配html5實(shí)現(xiàn)照片視頻上傳

二、分析實(shí)現(xiàn)方法:

由于不懂前端開(kāi)發(fā),不知道H5中有 input file之類的標(biāo)簽控件,可以用來(lái)選擇文件,剛開(kāi)始的思路還是想著native 端是否要通過(guò)提供inputstream流方式,將文件內(nèi)容傳遞給JS。后來(lái)和前端溝通之后,H5在電腦端都是用input 設(shè)置type為 file 來(lái)實(shí)現(xiàn)文件選擇功能,于是才開(kāi)始搜索資料,發(fā)現(xiàn)時(shí)需要在webview中設(shè)置  setWebChromeClient ,其中有對(duì)input 的響應(yīng)回調(diào):

三、具體實(shí)現(xiàn):

前端代碼:

<input type="file" accept="*/*" name="choose file">
<input type="file" accept="image/*" name="choose image">
<input type="file" accept="video/*" name="choose video">
<input type="file" accept="image/example" name="take photo and upload image">
<input type="file" accept="video/example" name="take video and upload video">

native端代碼:

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean onShowFileChooser(WebView webView,
                                 ValueCallback<Uri[]> filePathCallback,
                                 WebChromeClient.FileChooserParams fileChooserParams) {
    mFilePathCallbacks = filePathCallback;
    // TODO: 根據(jù)標(biāo)簽中得接收類型,啟動(dòng)對(duì)應(yīng)的文件類型選擇器
    String[] acceptTypes = fileChooserParams.getAcceptTypes();
    for (String type : acceptTypes) {
        Log.d(TAG, "acceptTypes=" + type);
    }
    // 針對(duì)拍照后馬上進(jìn)入上傳狀態(tài)處理
    if ((acceptTypes.length > 0) && acceptTypes[0].equals("image/example")) {
        Log.d(TAG, "onShowFileChooser takePhoto");
        Intent it = CameraFunction.takePhoto(mContext);
        startActivityForResult(it, TAKE_PHOTO_AND_UPLOAD_REQUEST);
        return true;
    }

    // 針對(duì)錄像后馬上進(jìn)入上傳狀態(tài)處理
    if ((acceptTypes.length > 0) && acceptTypes[0].equals("video/example")) {
        Log.d(TAG, "onShowFileChooser record video");
        Intent it = CameraFunction.recordVideo(mContext);
        startActivityForResult(it, RECORD_VIDEO_AND_UPLOAD_REQUEST);
        return true;
    }

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    if (acceptTypes.length > 0) {
        if (acceptTypes[0].contains("image")) {
            intent.setType("image/*");
        } else if (acceptTypes[0].contains("video")) {
            intent.setType("video/*");
        } else {
            intent.setType("*/*");
        }
    } else {
        intent.setType("*/*");
    }

    WebViewActivity.this.startActivityForResult(Intent.createChooser(intent, "File Chooser"),
            REQUEST_FILE_PICKER);
    return true;
}

回調(diào)設(shè)置uri:

/**
 * 設(shè)置input 標(biāo)簽出發(fā)的回調(diào)選擇文件路徑,優(yōu)先使用path參數(shù),
 * 其次使用uri參數(shù)
 * @param uriParam
 * @param pathParam
 */
private void setFilePathCallback(Uri uriParam, String pathParam) {
    //都為空,則設(shè)置null
    if (uriParam == null && pathParam == null) {
        if (mFilePathCallback != null) {
            mFilePathCallback.onReceiveValue(null);
        }
        if (mFilePathCallbacks != null) {
            mFilePathCallbacks.onReceiveValue(null);
        }
    } else if (null != pathParam) { // 優(yōu)先使用path
        if (mFilePathCallback != null) {
            Uri uri = Uri.fromFile(new File(pathParam));
            mFilePathCallback.onReceiveValue(uri);
        }
        if (mFilePathCallbacks != null) {
            Uri uri = Uri.fromFile(new File(pathParam));
            mFilePathCallbacks.onReceiveValue(new Uri[] { uri });
        }
    } else if (null != uriParam) { //其次使用uri
        if (mFilePathCallback != null) {
            String path = UriUtils.getPath(getApplicationContext(), uriParam);
            Uri uri = Uri.fromFile(new File(path));
            mFilePathCallback.onReceiveValue(uri);
        }
        if (mFilePathCallbacks != null) {
            String path = UriUtils.getPath(getApplicationContext(), uriParam);
            Uri uri = Uri.fromFile(new File(path));
            mFilePathCallbacks.onReceiveValue(new Uri[] { uri });
        }
    }

    mFilePathCallback = null;
    mFilePathCallbacks = null;

}

針對(duì)各個(gè)請(qǐng)求場(chǎng)景進(jìn)行處理:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {

關(guān)于“webview怎么適配html5實(shí)現(xiàn)照片視頻上傳”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“webview怎么適配html5實(shí)現(xiàn)照片視頻上傳”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI