Splash启动页后台配置图片的处理
2017-06-20 23:38
120 查看
一个很简单的需求,启动页背景图片是后台配置的,客户端需要做的就是拉取图片使用
1.我的处理是将图片保存到本地,在应用启动时读取图片
2.看代码实现
3.启动页读取图片
1.我的处理是将图片保存到本地,在应用启动时读取图片
2.看代码实现
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { downLoadSplashAd(); } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults); if (requestCode == 1) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { downLoadSplashAd(); } } }
public void downLoadSplashAd() { //启动页广告 String dataString = FSUserConfig.getDataString(mContext, FSUserConfig.JKEY_AD_CONFIG_0001, ""); LogUtils.d("download", "下载图片: " + dataString); if (dataString.length() > 0) { try { JSONArray array = new JSONArray(dataString); for (int i = 0; i < array.length(); i++) { JSONObject object = (JSONObject) array.get(i); String image_url = object.getString("image_url"); saveUrlImg(image_url); } } catch (JSONException e) { e.printStackTrace(); } } }
private void saveUrlImg(String image_url) { new GetImg().execute(image_url); }
/** * 将url转换成bitmap异步任务 */ private class GetImg extends AsyncTask<String, Void, Bitmap> { @Override protected Bitmap doInBackground(String... strings) { HttpURLConnection con = null;//访问网络 InputStream is = null; Bitmap bitmap = null; try { URL url = new URL(strings[0]); con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(5 * 1000); con.setReadTimeout(5 * 1000); /*http 响应吗 * 200:成功 * 404:未找到 * 500:发生错误 */ if (con.getResponseCode() == 200) { is = con.getInputStream(); bitmap = BitmapFactory.decodeStream(is); return bitmap; } } catch (Exception e) { e.printStackTrace(); } finally { try { if (is != null) { is.close(); } } catch (IOException e) { e.printStackTrace(); } if (con != null) { con.disconnect(); } } return null; } // onPostExecute在UI线程中执行命令 @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); String path = Environment.getExternalStorageDirectory().toString() + "/fs_img/chache"; File path1 = new File(path); if (!path1.exists()) { path1.mkdirs(); } //TODO 暂时只考虑保存一张图片 File file = new File(path1, "splash.jpg"); String filePath = file.getAbsolutePath(); saveFileByBitmap(bitmap, filePath); LogUtils.d("download", "保存路径:" + filePath); } }
/** * 保存图片到本地 * * @param bitmap * @param newImagePath */ private void saveFileByBitmap(Bitmap bitmap, String newImagePath) { File file = new File(newImagePath); try { FileOutputStream out = new FileOutputStream(file); if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) { out.flush(); out.close(); //保存图片路径 FSUserConfig.setData(mContext, GlobalVariables.JKEY_AD_SPLASHIMAG_PATH, newImagePath); } } catch (Exception e) { e.printStackTrace(); } }
3.启动页读取图片
//显示后台配置启动页广告 String path = FSUserConfig.getDataString(this, GlobalVariables.JKEY_AD_SPLASHIMAG_PATH, "") 4000 ; if (VsUtil.fileIsExists(path)) { Bitmap bitmap = BitmapFactory.decodeFile(path); splash_iamg.setImageBitmap(bitmap); }
相关文章推荐
- imgAreaSelect做前台图片截取+java后台处理
- tomcat配置项目的图片路径不在项目下的处理
- KindEditor的使用和上传图片的后台处理
- tomcat配置项目的图片路径不在项目下的处理
- 【DWR】学习笔记(一):基本配置&&前后台的数据传输与处理
- FCKeditor是使用非常广泛的HTML编辑器,本文从 ASP.NET 的使用场景对 FCKeditor 与 FCKeditor.NET 的配置、功能扩展(如自定义文件上传子目录、自定义文件名、上传图片的后期处理等)、以及安全性进行初步的阐述。
- django/js 前后台对图片上传的处理
- Zp的Ext学习笔记(一)——坑爹的MVC(html中Extjs配置、proxy代理的前后台交互、json解析、后台处理传入json乱码)
- Ajax 图片上传 和 后台处理
- nginx图片过滤处理模块http_image_filter_module安装配置笔记
- nginx图片过滤处理模块http_image_filter_module安装配置笔记
- java应用程序中处理配置文件和图片路径在打包后路径不对的问题
- PhoneGap splash 欢迎页 启动页 黑屏处理
- nginx图片过滤处理模块http_image_filter_module安装配置笔记
- 后台使用imagemagick的convert命令来处理图片真是太方便了。
- tomcat配置项目的图片路径不在项目下的处理
- KindEditor的使用和上传图片的后台处理
- 上传图片+浏览+裁切 Demo(无后台处理部分)
- AFNetworking与MJExtension处理后台_图片框架SDWebImage(初稿)
- java后台 处理图片大小