有一個需求是app的幫助文檔是word格式,ios可以直接用webview加載word顯示,Android不行。而美工不配合轉換成圖片,前端沒時間把word寫成html
沒辦法,自己搞。
步驟:
1、用第三方靠譜的軟件把word文檔轉成html。
2、放入assets目錄下,用webview進行加載。
3、然后你會發現里面的圖片顯示不了,傻眼了,為什么?? html的image標簽中src指向的地址中沒有圖片資源啊。你的工程中沒有圖片資源啊,怎么辦?
4、可以把圖片存入工程中,或者本地等等,然后改html中的src路徑。 但是。。。。。俺家的幫助文檔有200張圖片。一條條改真的好嗎。
5、解決方案:把圖片放入assets目錄下的一個文件夾中。然后命名要跟html中的圖片命名相同,然后利用webview動態加載顯示。代碼如下
public void setWebViewListener() { //不調用系統瀏覽器 webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); } @Override public void onLoadResource(WebView view, String url) { super.onLoadResource(view, url); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); } @Override public void onPageCommitVisible(WebView view, String url) { super.onPageCommitVisible(view, url); } @Override public WebResourceResponse shouldInterceptRequest(WebView view, String request) {//主要是用的這個方法,這個方法會在每次讀取圖片的時候回調,
//只要在這個時候返回要的圖片即可,之前之所以把圖片的名字命名成跟html中一樣的是為了少寫代碼 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { LogUtils.logInfoStar(request+"返回的request"); } AssetManager am = getResources().getAssets(); InputStream is = null; // String[] helpimgs = GetAssetsFiles.getfilesFromAssets(mContext, "helpimg"); String substring = request.substring(request.lastIndexOf("/")+1,request.lastIndexOf(".")); try { is = am.open("helpimg/"+substring+".png"); WebResourceResponse res=new WebResourceResponse("text/html","utf-8",is); return res; } catch (IOException e) { e.printStackTrace(); } return super.shouldInterceptRequest(view, request); } });
以下是總結的從assets目錄中讀取文件的工具類,請看詳細注釋:
package com.create.bicdroid.utils; import android.content.Context; import android.content.res.AssetManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import com.create.utilslibrary.LogUtils; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; /** * Created by Administrator on 2016/11/21. */ public class GetAssetsFiles { /** * 獲取assets目錄下的圖片 * * @param context * @param fileName * @return */ public static Bitmap getImageFromAssetsFile(Context context, String fileName) { Bitmap image = null; AssetManager am = context.getResources().getAssets(); try { InputStream is = am.open(fileName); image = BitmapFactory.decodeStream(is); is.close(); } catch (IOException e) { e.printStackTrace(); } return image; } /** * 獲取assets目錄下的單個文件 * * @param context * @param fileName * @return */ public static File getFileFromAssetsFile(Context context, String fileName) {//這種方式不能用,只能用於webview加載,直接取路徑是不行的 String path = "file:///android_asset/" + fileName; File file = new File(path); return file; } /** * 獲取所有文件 * * @param path * @return */ public static String[] getfilesFromAssets(Context context, String path) { AssetManager assetManager = context.getAssets(); String[] files = null; try { files = assetManager.list(path); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } for (String str : files) { LogUtils.logInfoStar(str); } return files; } /** * 將assets下的文件放到sd指定目錄下 * * @param context 上下文 * @param assetsPath assets下的路徑 * @param sdCardPath sd卡的路徑 */ public static void putAssetsToSDCard(Context context, String assetsPath, String sdCardPath) { try { String mString[] = context.getAssets().list(assetsPath); if (mString.length == 0) { // 說明assetsPath為空,或者assetsPath是一個文件 InputStream mIs = context.getAssets().open(assetsPath); // 讀取流 byte[] mByte = new byte[1024]; int bt = 0; File file = new File(sdCardPath + File.separator + assetsPath.substring(assetsPath.lastIndexOf('/'))); if (!file.exists()) { file.createNewFile(); // 創建文件 } else { return;//已經存在直接退出 } FileOutputStream fos = new FileOutputStream(file); // 寫入流 while ((bt = mIs.read(mByte)) != -1) { // assets為文件,從文件中讀取流 fos.write(mByte, 0, bt);// 寫入流到文件中 } fos.flush();// 刷新緩沖區 mIs.close();// 關閉讀取流 fos.close();// 關閉寫入流 } else { // 當mString長度大於0,說明其為文件夾 sdCardPath = sdCardPath + File.separator + assetsPath; File file = new File(sdCardPath); if (!file.exists()) file.mkdirs(); // 在sd下創建目錄 for (String stringFile : mString) { // 進行遞歸 putAssetsToSDCard(context, assetsPath + File.separator + stringFile, sdCardPath); } } } catch ( Exception e ) { e.printStackTrace(); } } }