App下載

webview適配H5上有幾種照片視頻上傳方法? 照片或者視頻文件上傳的方法分享!

微醉陽(yáng)光 2021-08-17 10:39:36 瀏覽數(shù) (3833)
反饋

在平常的生活中我們會(huì)在喜歡的社交app中上傳有關(guān)于我們的各種生活日常,那么今天我們就來(lái)講講有關(guān)于:“webview適配H5上有幾種照片視頻上傳方法?”這個(gè)問(wèn)題的相關(guān)內(nèi)容分享!

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

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

 

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

由于不懂前端開(kāi)發(fā),不知道H5中有 input file之類(lèi)的標(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)簽中得接收類(lèi)型,啟動(dòng)對(duì)應(yīng)的文件類(lèi)型選擇器
    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) {

總結(jié):既然用H5開(kāi)發(fā)APP,就需要了解前端,不懂就要問(wèn)了。查詢(xún)方向要對(duì),否則南轅北轍,方向有時(shí)候比努力重要!

那么以上的話就是有關(guān)于:“webview適配H5上有幾種照片視頻上傳方法?”這個(gè)問(wèn)題的相關(guān)內(nèi)容分享,更多有關(guān)于html5這方面的相關(guān)內(nèi)容我們都可以在W3Cschool中進(jìn)行學(xué)習(xí)和了解!


0 人點(diǎn)贊