WebView加載附件,和pnd格式顯示

首先咱們要自定義一個獲取附件地址的工具類android

自定義橋接規則,判斷是不是本身的設定好的字段git

//HTML5跳轉工具
public class Html5URLJumpTool {

    //自定義截取字段,判斷是不是本身的規則
    public static final String clickprojectSaydetail = "clickprojectSaydetail";
    public static final String legitimateScheme = "haleyaction";//schmem標誌
    public static final String legitimateSchemeIfHttp = "http";//schmem標誌

    /**
     * 判斷scheme是否合法
     */
    public static boolean judgeSchemeIflegitimate(Uri uri){
        String scheme = uri.getScheme();
        if(!legitimateScheme.equals(scheme)){
            return false;
        }
        return true;
    }

    /**
     * 判斷scheme是不是http
     */
    public static boolean judgeSchemeIfHttp(Uri uri){
        String scheme = uri.getScheme();
        if(!legitimateSchemeIfHttp.equals(scheme)){
            return false;
        }
        return true;
    }

    /**
     * 跳轉URL
     */
    public static void jumpURL(String URL, Activity activity){
        //URL非空驗證
        if(TextUtils.isEmpty(URL)){
            return;
        }
        Uri interceptURL = Uri.parse(URL);
        //scheme合法驗證
        if(judgeSchemeIflegitimate(interceptURL)){
            String getAuthority = interceptURL.getAuthority();
            String getQuery = interceptURL.getQuery();
            implementJump(getAuthority,getQuery,activity);
            return;
        }
    }
    /**
     * 獲取鍵值對
     */
    public static Map<String,String> getQuery(String query){
        if(TextUtils.isEmpty(query)){
            return null;
        }
        Map<String,String> keyValueMap = new HashMap<String, String>();
        String[] keyMap = query.split("&");
        for(String keyMapstr :keyMap){
            String[] keyvalue = keyMapstr.split("=");
            if(keyvalue !=null && keyvalue.length == 2){
                keyValueMap.put(keyvalue[0],keyvalue[1]);
            }
        }
        return keyValueMap.size()>0?keyValueMap:null;
    }

    /**
     * 執行跳轉
     * ----getAuthority---clickplayReply---getQuery----id=52905445857005568&courseId=50455850270949376
     */
    public static void implementJump(String getAuthority,String getQuery,Activity activity){

        Intent intent = new Intent(activity, AnnexActivity.class);//設置跳轉附件的頁面
        Map<String,String> live_map = getQuery(getQuery);//獲取鍵值對
        for (String s: live_map.keySet()) {
            String s1 = live_map.get(s);
        }
        if(live_map !=null && live_map.size() >0 && live_map.containsKey("url")) {
            String url = live_map.get("url");
            intent.putExtra("url",url);
            activity.startActivity(intent);
        }

    }
}

 

而後設置點擊附件github

webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient() {
    //當web點擊觸發的時候觸發該方法
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Html5URLJumpTool.jumpURL(url,DetailsActivity.this);
        return false;
    }
});

最後設置跳轉頁面顯示附件web

webview.setWebViewClient(new AppWebViewClients());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUseWideViewPort(true);
webview.loadUrl("http://view.officeapps.live.com/op/view.aspx?src=" + url);

 

附件頁面app

附頁頁面設置一個webview就能夠ide

若是是.pdf格式,須要咱們引用第三方處理工具

首先導入依賴this

implementation  'com.github.barteksc:android-pdf-viewer:3.0.0-beta.5'

而後XML文件url

<com.github.barteksc.pdfviewer.PDFView
    android:id="@+id/web"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></com.github.barteksc.pdfviewer.PDFView>

最後orm

mWeb = (PDFView) findViewById(R.id.web);
new AsyncTask<String, Void, InputStream>() {
    @Override
    protected InputStream doInBackground(String... strs) {
        URL url = null;
        try {
            url = new URL(strs[0]);
            HttpURLConnection connection = (HttpURLConnection)
                    url.openConnection();
            connection.setRequestMethod("GET");//試過POST 可能報錯
            connection.setDoInput(true);
            connection.setConnectTimeout(10000);
            connection.setReadTimeout(10000);
            //實現鏈接
            connection.connect();

            if (connection.getResponseCode() == 200) {
                InputStream is = connection.getInputStream();
                //這裏給過去就好了
                return is;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(InputStream inputStream) {
        super.onPostExecute(inputStream);

        mWeb.fromStream(inputStream)
                .enableSwipe(true)
                .swipeHorizontal(false)
                .enableDoubletap(true)
                .defaultPage(0)
                .onPageScroll(new OnPageScrollListener() {
                    @Override
                    public void onPageScrolled(int page, float positionOffset) {

                    }
                })
                .onError(new OnErrorListener() {
                    @Override
                    public void onError(Throwable t) {
                        Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
                    }
                })
                .enableAnnotationRendering(false)
                .password(null)
                .scrollHandle(null)
                .load();
    }
}.execute(ur);
相關文章
相關標籤/搜索