webview中的img标签加载Android本地的图片
2018-02-24 10:34
435 查看
第一种:本地Html加载本地图片(不存在跨域问题)
注意问题:一定是file:///开头,注意是三个斜杠 例如:String path= "file://"+Environment.getExternalStorageDirectory()+ File.separator+"meimei.jpg";
代码如下:
第二种:非本地Html加载本地图片(存在跨域问题)
解决方式:
步骤一:对 img标签 增加 方法一:(未成功)
crossorigin="anonymous"属性
< img src="../img/addImg.png" alt="" style="width:100%;height:100%" crossorigin="anonymous">步骤二: 在
方法二:将图片转换为Base64 (成功)转为Base64 代码为:
注意问题:一定是file:///开头,注意是三个斜杠 例如:String path= "file://"+Environment.getExternalStorageDirectory()+ File.separator+"meimei.jpg";
代码如下:
package com.example.a5.myapplication; import android.os.Environment; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import java.io.File; public class MainActivity extends AppCompatActivity { private WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webView= (WebView) findViewById(R.id.webView); WebSettings webSettings=webView.getSettings(); //允许webview对文件的操作 webSettings.setAllowUniversalAccessFromFileURLs(true); webSettings.setAllowFileAccess(true); webSettings.setAllowFileAccessFromFileURLs(true); //用于js调用Android webSettings.setJavaScriptEnabled(true); //设置编码方式 webSettings.setDefaultTextEncodingName("utf-8"); webView.setWebChromeClient(new chromClient()); //访问Android assets文件夹内的 String url="file:///android_asset/test.html"; //访问网页Html // String url="http://192.168.1.121:8080/jsandroid/index.html"; runWebView(url); } private class chromClient extends WebChromeClient{ @Override public void onProgressChanged(WebView view, int newProgress) { if(newProgress==100){ //页面加载完成执行的操作 String path= "file://"+Environment.getExternalStorageDirectory()+ File.separator+"123.jpg"; String action="javascript:aa('"+path+"')"; new AlertDialog.Builder(MainActivity.this) .setMessage(action) .show(); runWebView(action); } super.onProgressChanged(view, newProgress); } } private void runWebView(final String url){ runOnUiThread(new Runnable() { @Override public void run() { webView.loadUrl(url); } }); } }
第二种:非本地Html加载本地图片(存在跨域问题)
解决方式:
步骤一:对 img标签 增加 方法一:(未成功)
crossorigin="anonymous"属性
< img src="../img/addImg.png" alt="" style="width:100%;height:100%" crossorigin="anonymous">步骤二: 在
webView.loadUrl(url)之前增加:
try { if (Build.VERSION.SDK_INT >= 16) { Class<?> clazz = hybridWebView.getSettings().getClass(); Method method = clazz.getMethod( "setAllowUniversalAccessFromFileURLs", boolean.class); if (method != null) { method.invoke(hybridWebView.getSettings(), true); } } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); }最后:webView.loadUrl(url)
方法二:将图片转换为Base64 (成功)转为Base64 代码为:
package com.channelsoft.livev114.utils; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Base64; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /** * <pre> * author: zhaojz * crateTime :2018/2/24 * email :zhaojz@channelsoft.com * desc : 描述 * mender:暂无其他修改者 * modificationTime:修改时间 * modificationDesc:暂无修改描述 * </pre> */ public class ReadImgToBinary2 { /** * * @param imgPath * @param bitmap * @param imgFormat 图片格式 * @return */ public static String imgToBase64(String imgPath, Bitmap bitmap, String imgFormat) { if (imgPath !=null && imgPath.length() > 0) { bitmap = readBitmap(imgPath); } if(bitmap == null){ //bitmap not found!! } ByteArrayOutputStream out = null; try { out = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); byte[] imgBytes = out.toByteArray(); return Base64.encodeToString(imgBytes, Base64.DEFAULT); } catch (Exception e) { // TODO Auto-generated catch block return null; } finally { try { out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } private static Bitmap readBitmap(String imgPath) { try { return BitmapFactory.decodeFile(imgPath); } catch (Exception e) { // TODO Auto-generated catch block return null; } } /** * * @param base64Data * @param imgName * @param imgFormat 图片格式 */ public static void base64ToBitmap(String base64Data,String imgName,String imgFormat) { byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT); Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); File myCaptureFile = new File("/sdcard/", imgName); FileOutputStream fos = null; try { fos = new FileOutputStream(myCaptureFile); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } boolean isTu = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); if (isTu) { // fos.notifyAll(); try { fos.flush(); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }webView调用代码:
String urlBase64 = ReadImgToBinary2.imgToBase64(images.get(0).path,null,""); String urlBase64PosterImg = "data:image/png;base64,"+urlBase64; hybridWebView.loadUrl("javascript:posterImg('" + urlBase64PosterImg+ "')");如有问题,欢迎群聊:659014357
相关文章推荐
- Android开发实现webview中img标签加载本地图片的方法
- webview中的img标签加载Android本地的图片
- Android使用WebView加载本地图片终极大杀招
- Android开发 WebView加载从本地加载图片
- android WebView加载html 并 引用本地资源(图片、字体库)
- android WebView 控件加载本地sdcard中html文件图片的问题
- Android webView 加载网页时,使用本地图片替换网页内的图片
- Android用webView加载h5的页面,h5获取本地的图库中的图片
- Android webView 加载网页时,使用本地图片替换网页内的图片
- Android webview加载本地图片
- android webview 加载本地图片
- android WebView 控件加载本地sdcard中html文件图片的问题
- Android webview加载本地图片
- Android webView 加载网页时,使用本地图片替换网页内的图片
- android中webview空间通过Img 标签显示sd卡中 的图片
- Android - webview拦截css,js,图片后加载本地外部存储的文件(shouldOverrideUrlLoading)
- Android webView 加载网页时,使用本地图片替换网页内的图片
- Android的webview加载本地html、本apk内html和远程URL
- Android的webview加载本地html、本apk内html和远程URL
- Android使用WebView加载本地资源