由于近由于工作需要,要实现在已经发布的APP上实现更新启动图片,发现网上没有这块的信息,虽然实现方式比较简单,但还是写下来,供参考;
1、首先,用一个接口访问网络,下载新的启动画面的图片,我用的是ImageView里面的一张画面来实现的,代码如下:
View tempView = mGridView.getChildAt(0); ImageView imageView = (ImageView) tempView .findViewById(R.id.gv_image_detail); File file = ImageUtils.saveSplashImageToSdCard(imageView);
public static final File saveSplashImageToSdCard(ImageView image) { boolean success = false; String imageName = "splash.jpg"; File storeDir = AppData.getContext().getExternalFilesDir(null); File imageFile = new File(storeDir, imageName); F.makeLog(imageFile.toString()); BitmapDrawable drawable = (BitmapDrawable) image.getDrawable(); FileOutputStream outStream; Bitmap bitmap = drawable.getBitmap(); try { outStream = new FileOutputStream(imageFile); bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); // 100 to keep full quality of the image outStream.flush(); outStream.close(); success = true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (success) { // Toast.makeText(getApplicationContext(), "Image saved with success", // Toast.LENGTH_LONG).show(); F.makeLog("success save to sd card"); UserInfo.saveSplashPath(imageFile.toString()); return imageFile; } else { return null; } }
public static void saveSplashPath(String msg) { SharedPreferences sharedPreferences = AppData.getContext() .getSharedPreferences(USER_INFO_TAG, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString(FILE_TAG, msg); editor.putBoolean(SPLASH_TAG, true); editor.commit(); } public static boolean isSplashImageChange() { SharedPreferences sharedPreferences = AppData.getContext() .getSharedPreferences(USER_INFO_TAG, Context.MODE_PRIVATE); boolean change = sharedPreferences.getBoolean(SPLASH_TAG, false); return change; } public static String getSplashImagePath() { SharedPreferences sharedPreferences = AppData.getContext() .getSharedPreferences(USER_INFO_TAG, Context.MODE_PRIVATE); String path = sharedPreferences.getString(FILE_TAG, "0"); return path; }
if (UserInfo.isSplashImageChange()) { String file = UserInfo.getSplashImagePath(); F.makeLog(file.toString()); // F.makeToast(file.toString()); Bitmap bitmap = BitmapFactory.decodeFile(file); imageView.setImageBitmap(bitmap); }
由于是上班时间,偷偷来更新博客的,写的很是简略,不过应该可以看懂的吧,代码是集成在公司的项目中,不好上源代码,望海涵。