webview h5頁面調取本地相冊


效果圖
public class WebViewActivity extends Activity {
    @BindView(R.id.constraintLayout_Title_Top)
    ConstraintLayout_Title_Top constraintLayoutTitleTop;
    @BindView(R.id.webview)
    com.tencent.smtt.sdk.WebView webview;
    public ValueCallback<Uri[]> mUploadMessageForAndroid5;
    public ValueCallback<Uri> mUploadMessage;
    public final static int FILE_CHOOSER_RESULT_CODE_FOR_ANDROID_5 = 2;
    private final static int FILE_CHOOSER_RESULT_CODE = 1;// 表單的結果回調

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        ButterKnife.bind(this);

        initWebView();
        initTitle();
    }
    //webview設置
    private void initWebView(){
        webview.getSettings().setTextZoom(100);//當前字體百分比
        webview.getSettings().setDefaultTextEncodingName("utf-8");//指定編碼方式
        webview.getSettings().setJavaScriptEnabled(true);//支持js
        webview.getSettings().setDomStorageEnabled(true); //設置支持DomStorage
        webview.getSettings().setAllowFileAccess(true);//設置在WebView內部是否允許訪問文件
        webview.getSettings().setBlockNetworkLoads(false);//設置WebView是否從網絡加載資源,Application需要設置訪問網絡權限,否則報異常
        webview.getSettings().setBlockNetworkImage(false);//設置WebView是否以http、https方式訪問從網絡加載圖片資源,默認false
        webview.loadUrl("http://ldj.oapi.xunmall.com:3038/service/bussiness?uid=1440&f=1&token=3e2f600c5437f083ab93cf19b871dc51&v=58");
        // 給WebView設置監聽
        webview.setWebViewClient(new WebViewClient() {
            //跳轉連接
            @Override
            public boolean shouldOverrideUrlLoading(com.tencent.smtt.sdk.WebView view, String url) {
                // 所有連接強制在當前WeiView加載,不跳服務器
                webview.loadUrl(url);
                return true;
            }
            //加載結束
            @Override
            public void onPageFinished(com.tencent.smtt.sdk.WebView view, String url) {
                super.onPageFinished(view, url);
                webview.loadUrl("javascript:openMusic()");//加載結束,加載音樂
            }
        });
        webview.setWebChromeClient(new WebChromeClient() {
        //Android < 5.0
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                openFileChooserImpl(uploadMsg);
             }
        //Android => 5.0
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> uploadMsg, WebChromeClient.FileChooserParams fileChooserParams) {
                onenFileChooseImpleForAndroid(uploadMsg);
                return true;
            }
        });
    }
    //標題欄設置
    private void initTitle(){
        //back鍵
        constraintLayoutTitleTop.setBackOnlickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        constraintLayoutTitleTop.setTitleName("webview頁面");
    }
    /**
     * android 5.0 以下開啟圖片選擇(原生)
     * 可以自己改圖片選擇框架。
     */
    private void openFileChooserImpl(ValueCallback<Uri> uploadMsg) {
        mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        startActivityForResult(Intent.createChooser(i, "File Chooser"),
                FILE_CHOOSER_RESULT_CODE);
    }
    /**
     * android 5.0(含) 以上開啟圖片選擇(原生)
     * 可以自己改圖片選擇框架。
     */
    private void onenFileChooseImpleForAndroid(ValueCallback<Uri[]> filePathCallback) {
        mUploadMessageForAndroid5 = filePathCallback;
        Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
        contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
        contentSelectionIntent.setType("image/*");
        Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
        chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
        chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
        startActivityForResult(chooserIntent, FILE_CHOOSER_RESULT_CODE_FOR_ANDROID_5);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode,Intent intent) {
        Uri result = (intent == null || resultCode != Activity.RESULT_OK) ? null: intent.getData();
        switch (requestCode){
            case FILE_CHOOSER_RESULT_CODE:  //android 5.0以下 選擇圖片回調
                if (null == mUploadMessage)
                    return;
                mUploadMessage.onReceiveValue(result);
                mUploadMessage = null;
                break;
            case FILE_CHOOSER_RESULT_CODE_FOR_ANDROID_5:  //android 5.0(含) 以上 選擇圖片回調
                if (null == mUploadMessageForAndroid5)
                    return;
                if (result != null) {
                    mUploadMessageForAndroid5.onReceiveValue(new Uri[]{result});
                } else {
                    mUploadMessageForAndroid5.onReceiveValue(new Uri[]{});
                }
                mUploadMessageForAndroid5 = null;
                break;
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM