Splash启动页后台配置图片的处理
来源:互联网 发布:iso软件开发 编辑:程序博客网 时间:2024/05/18 03:17
一个很简单的需求,启动页背景图片是后台配置的,客户端需要做的就是拉取图片使用
- 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, ""); if (VsUtil.fileIsExists(path)) { Bitmap bitmap = BitmapFactory.decodeFile(path); splash_iamg.setImageBitmap(bitmap); }
阅读全文
0 0
- Splash启动页后台配置图片的处理
- 关于应用的Splash启动页
- PhoneGap splash 欢迎页 启动页 黑屏处理
- Android 处理启动页(Splash)白屏问题
- android的splash启动画面
- 高效启动页(Splash)
- Android - 手机淘宝启动页(Splash)是如何实现的?
- 安卓制作瞬间响应的splash启动页
- 给使用phonegap的Android程序加上splash screen(启动图片 预览图))
- 后台处理批量上传图片的方法
- 后台处理base_64图片的方法
- android设置启动splash图片(消除启动白屏)不使用Appcompat主题设置的方法
- 程序启动的Splash screen与跳转
- Splash-运行软件前的图片
- Unity 安卓启动图片(splash image)前黑屏解决方法
- 后台处理图片选择器
- Splash界面/启动界面中处理耗时任务
- android网络图片Splash欢迎页
- suse Linux下安装SVN客户端
- 用JAVASwing实现模拟磁盘调度
- <Effective C++> Item 29:争取异常安全的代码
- 邹博机器学习代码分析(1)-线性回归
- C++每日一课(一)
- Splash启动页后台配置图片的处理
- python:对象属性与类属性
- Effective Python 第一章回顾
- 1009. 说反话 (20)
- poj1061青蛙的约会 同余方程
- MySQL登录密码错误
- VB脚本:快速入门教程
- pycharm安装配置篇
- jQuery日历价格、库存设置Web组件2,前后台适用,可自定义字段及颜色风格