博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
公司--下载svg图片
阅读量:4315 次
发布时间:2019-06-06

本文共 8130 字,大约阅读时间需要 27 分钟。

加载本地svg图片:

SVGParserRenderer norDrawable = OtherPageConfigsManager.getInstance().getSVGParserRenderer(this,map.get("iconUrlNor"));public SVGParserRenderer getSVGParserRenderer(Context context, String svgPath){    String config = "";    if(iconLoadPathType == SAVE_TYPE_ASSETS)//本地存储位置        config = FileSystem.getFromAssets(context, "panelConfigFolder/"+svgPath);    else if(iconLoadPathType == SAVE_TYPE_SDCARD)//SDK存储位置        config = "";//"file://"+FileSystem.getCacheRootDir(context, "").getPath()+ url;    else if(iconLoadPathType == SAVE_TYPE_SYSTEM_DATA_FOLDER)//内部文件系统存储位置        //modify by chris.lei  添加中台未配发右侧在线客服图标导致从内部文件中读取不到信息客户端首页显示空白的问题        config = FileSystem.readFromFile(context,                 getConfigFile(context, "panelConfigFolder", svgPath).getPath());        if(config == ""){            config = FileSystem.getFromAssets(context, "panelConfigFolder/"+svgPath);        }    return SvgRes1.getSVGParserRenderer(context, config);//从配置文件获取}        public static SVGParserRenderer getSVGParserRenderer(Context context, String svgContent) {    return svgContent != null && !svgContent.equals("")?new SVGParserRenderer(context, parseCssStyleToSvg(svgContent)):null;}public static String parseCssStyleToSvg(String svgContent) {    int startIndex = svgContent.indexOf("
") + "".length(); String styleStr = null; if(startIndex >= 0 && endIndex >= 0) { styleStr = svgContent.substring(startIndex, endIndex); // styleStr =
if(styleStr != null && !styleStr.equals("")) { int startIndex1 = styleStr.indexOf("."); int endIndex1 = styleStr.lastIndexOf("."); int endendIndex = styleStr.lastIndexOf(";}") + ";}".length(); String colorStr; if(startIndex1 == endIndex1) { colorStr = styleStr.substring(startIndex1 + ".".length(), endendIndex); } else { colorStr = styleStr.substring(startIndex1, endendIndex); } //colorStr = strokeColor{fill:#B0B0B0;} String[] fillColorArr = colorStr.split(";"); if(fillColorArr != null) { for(int i = 0; i < fillColorArr.length; ++i) { if(fillColorArr[i].contains("fill:")) { String[] nameColorArr = fillColorArr[i].split("fill:"); if(nameColorArr != null) { int nameStartIndex = nameColorArr[0].indexOf(".") + ".".length(); String name = nameColorArr[0].substring(nameStartIndex, nameColorArr[0].length() - 1); String color = nameColorArr[1]; if(svgContent.contains("class=\"" + name + "\"")) { svgContent = svgContent.replace("class=\"" + name + "\"", "fill=\"" + color + "\""); } } } } } } svgContent = svgContent.replace(styleStr, ""); } return svgContent;} parseCssStyleToSvg的行参 svgContent =
返回 svgContent =

下载网络svg图片:

if (norDrawable == null) {    panelConfigsDownloader.startDownloadForSvgIcon(this,            OtherPageConfigsManager.getInstance(),            map.get("downloadUrl") + norFilePath, saveNorFilePath,            new SvgIconOnDownloadCompleteListener(                    mKdsShortcutView[index], map, "iconUrlNor"));}public void startDownloadForSvgIcon(Context context,ConfigsManager configsManager,String svgDownloadUrl,                                     String svgFilePath,                                    OnDownloadCompleteListener onDownloadCompleteListener){//下载配置文件String parentFolder = configsManager.mConfigInfo.saveFolderName+"/";    //parentFolder = panelConfigFolder/String fileName = "";if(svgFilePath != null && !svgFilePath.equals("")){    //svgFilePath = ueditor/1431988818264.svg    String[] saveFileDir = svgFilePath.split("/");    for(int i = 0; i < saveFileDir.length; i++){        if(saveFileDir[i].contains(".")){            fileName = saveFileDir[i];   //fileName = 1431988818264.svg            continue;        }        parentFolder += saveFileDir[i];  //parentFolder = panelConfigFolder/ueditor    }}else{    return;//说明不是需要下载的文件}File file = configsManager.getConfigFile(context, parentFolder, fileName);   //file = panelConfigFolder/ueditor/1431988818264.svgDownloadConfigFileThread downloadConfigFileThread =         new DownloadConfigFileThread(context, svgDownloadUrl, file,         onDownloadCompleteListener); if(!file.isDirectory() && !file.exists()) {
//考虑已经存在同样名字的文件或者目录, //Logger.d("downloadConfigFile", "startDownloadForSvgIcon exists:"+file.exists()); downloadConfigFileThread.start();//开始下载 }else{ Logger.i("快捷按钮配置文件", "警告:该文件存在相同文件名,不进行下载更新,请知晓!"); downloadConfigFileThread.onDownloadComplete();//已经存在也视为完成 }}public class DownloadConfigFileThread extends Thread{ public synchronized void run() { try { URL url = new URL(downurl);// http://113.78.134.110:21800/api/config/app/ui/otherpage/online/66099/100000 // 创建连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.connect(); // 获取文件大小 int length = conn.getContentLength();//40674 // 创建输入流 InputStream is = conn.getInputStream(); //处理文件路径不存在的问题 String dirFilePath = configFile.getPath();// /data/data/dongzheng.szkingdom.android.phone/files/panelConfigFolder/otherpage_temp.json String childDirPath = ""; String[] path = dirFilePath.split("/"); for(int i = 0; i < path.length-1;i++){ if(path[i].contains(".")) continue; childDirPath += "/" + path[i]; File file = new File(childDirPath); if(!file.exists()) {
//考虑已经存在同样名字的文件或者目录, Logger.d("tag", "DownloadConfigFileThread mkdir newPath:"+file.getPath()); file.mkdir(); file.setExecutable(true, false); file.setReadable(true, false); file.setWritable(true, false); } } FileOutputStream fos = new FileOutputStream(configFile); // 输出到文件 /data/data/dongzheng.szkingdom.android.phone/files/panelConfigFolder/otherpage_temp.json // 缓存 byte buf[] = new byte[1024]; int count = 0; // 写入到文件中 do { int numread = is.read(buf); count += numread; // 计算进度条位置 int progress = (int) (((float) count / length) * 100); float tempSpace = count / 1024.0f / 1024.0f; String downloadSpace = ""; try{ downloadSpace = String.format("%.3f",tempSpace)+"M";// > 1.0f ? (tempSpace+"M") : (count / 1024.0f+"KB"); }catch(Exception e){ } if (numread <= 0) { //下载完成 mHandler.removeMessages(0); Message msg = Message.obtain(); msg.what = 0; mHandler.sendMessage(msg); break; } // 写入文件 fos.write(buf, 0, numread); } while (true);// 点击取消就停止下载. fos.close(); is.close(); }catch(Exception e){ mHandler.removeMessages(1); mHandler.sendEmptyMessage(1); //e.printStackTrace(); } }}

 

转载于:https://www.cnblogs.com/yaowen/p/5660310.html

你可能感兴趣的文章
spring中的ResourceBundleMessageSource使用和测试示例
查看>>
css规范 - bem
查看>>
SQL 通用数据类型
查看>>
vscode - emmet失效?
查看>>
PHP高级教程-文件
查看>>
数据分页处理系列之一:Oracle表数据分页检索SQL
查看>>
UVALive 6145 Version Controlled IDE(可持久化treap、rope)
查看>>
mysql 将两个有主键的表合并到一起
查看>>
底部导航栏-----FragmentTabHost
查看>>
在linux中安装jdk以及tomcat并shell脚本关闭启动的进程
查看>>
apk,task,android:process与android:sharedUserId的区别
查看>>
MySQL 同主机不同数据库之间的复制
查看>>
iPad编程
查看>>
php编程安全指南
查看>>
在每天黄金时刻将数据库中数据获取包装成Excel表
查看>>
SpringBoot中使用Shiro和JWT做认证和鉴权
查看>>
SpringMVC和Struts2区别比较
查看>>
如何获得被动收入,工资之外再拿一份钱
查看>>
在一个整数区间里产生一组不重复的随机数
查看>>
Java系列之:看似简单的问题 静态方法和实例化方法的区别
查看>>