婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 關于webview適配H5上傳照片或者視頻文件的方法

關于webview適配H5上傳照片或者視頻文件的方法

熱門標簽:高德地圖標注廁所 地圖標注沿海城市房價 通遼地圖標注app 威海語音外呼系統(tǒng)平臺 西安金倫外呼系統(tǒng) 地圖標注員工作內(nèi)容 智能語音電銷機器人客戶端 中國地圖標注城市的 江西ai電銷機器人如何

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

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

 

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

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

三、具體實現(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ù)標簽中得接收類型,啟動對應的文件類型選擇器
    String[] acceptTypes = fileChooserParams.getAcceptTypes();
    for (String type : acceptTypes) {
        Log.d(TAG, "acceptTypes=" + type);
    }
    // 針對拍照后馬上進入上傳狀態(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;
    }

    // 針對錄像后馬上進入上傳狀態(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)設置uri

/**
 * 設置input 標簽出發(fā)的回調(diào)選擇文件路徑,優(yōu)先使用path參數(shù),
 * 其次使用uri參數(shù)
 * @param uriParam
 * @param pathParam
 */
private void setFilePathCallback(Uri uriParam, String pathParam) {
    //都為空,則設置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;

}

針對各個請求場景進行處理:

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

總結:既然用H5開發(fā)APP,就需要了解前端,不懂就要問了。查詢方向要對,否則南轅北轍,方向有時候比努力重要!

到此這篇關于關于webview適配H5上傳照片或者視頻文件的方法的文章就介紹到這了,更多相關webview適配H5上傳照片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持腳本之家!

標簽:眉山 北海 營口 阜陽 河池 青海 晉中 崇左

巨人網(wǎng)絡通訊聲明:本文標題《關于webview適配H5上傳照片或者視頻文件的方法》,本文關鍵詞  關于,webview,適配,上傳,照片,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《關于webview適配H5上傳照片或者視頻文件的方法》相關的同類信息!
  • 本頁收集關于關于webview適配H5上傳照片或者視頻文件的方法的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 新郑市| 定日县| 沧源| 始兴县| 屏山县| 平塘县| 城固县| 即墨市| 峨边| 远安县| 菏泽市| 松江区| 弥渡县| 青铜峡市| 子洲县| 舞阳县| 定远县| 且末县| 贡山| 柞水县| 手机| 凤庆县| 宜阳县| 灵台县| 延安市| 招远市| 揭阳市| 简阳市| 香河县| 五原县| 长治县| 龙井市| 垫江县| 建瓯市| 怀柔区| 阿拉尔市| 普陀区| 睢宁县| 平阴县| 靖宇县| 卓尼县|