Android开发:已经发布的APP,如何更改启动图片

来源:互联网 发布:win7红警网络进不去 编辑:程序博客网 时间:2024/06/06 05:08

  1. 由于近由于工作需要,要实现在已经发布的APP上实现更新启动图片,发现网上没有这块的信息,虽然实现方式比较简单,但还是写下来,供参考;  

1、首先,用一个接口访问网络,下载新的启动画面的图片,我用的是ImageView里面的一张画面来实现的,代码如下:


[java] view plaincopy
  1. View tempView = mGridView.getChildAt(0);  
  2.         ImageView imageView = (ImageView) tempView  
  3.                 .findViewById(R.id.gv_image_detail);  
  4.         File file = ImageUtils.saveSplashImageToSdCard(imageView);  

2、把取到的图片存储在本地目录:


[java] view plaincopy
  1. public static final File saveSplashImageToSdCard(ImageView image) {  
  2.         boolean success = false;  
  3.         String imageName = "splash.jpg";  
  4.         File storeDir = AppData.getContext().getExternalFilesDir(null);  
  5.         File imageFile = new File(storeDir, imageName);  
  6.         F.makeLog(imageFile.toString());  
  7.         BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();  
  8.         FileOutputStream outStream;  
  9.         Bitmap bitmap = drawable.getBitmap();  
  10.         try {  
  11.             outStream = new FileOutputStream(imageFile);  
  12.             bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);  
  13. //           100 to keep full quality of the image   
  14.             outStream.flush();  
  15.             outStream.close();  
  16.             success = true;  
  17.         } catch (FileNotFoundException e) {  
  18.             e.printStackTrace();  
  19.         } catch (IOException e) {  
  20.             e.printStackTrace();  
  21.         }  
  22.         if (success) {  
  23. //          Toast.makeText(getApplicationContext(), "Image saved with success",  
  24. //                  Toast.LENGTH_LONG).show();  
  25.             F.makeLog("success save to sd card");  
  26.             UserInfo.saveSplashPath(imageFile.toString());  
  27.             return imageFile;  
  28.         } else {  
  29.                     return null;  
  30.         }  
  31.     }  

3、建立一个sharepreference,保存存储的路劲和更新启动画面的判断;

[java] view plaincopy
  1. public static void saveSplashPath(String msg) {  
  2.         SharedPreferences sharedPreferences = AppData.getContext()  
  3.                 .getSharedPreferences(USER_INFO_TAG, Context.MODE_PRIVATE);  
  4.         SharedPreferences.Editor editor = sharedPreferences.edit();  
  5.         editor.putString(FILE_TAG, msg);  
  6.         editor.putBoolean(SPLASH_TAG, true);  
  7.         editor.commit();  
  8.     }  
  9.       
  10.     public static boolean isSplashImageChange() {  
  11.         SharedPreferences sharedPreferences = AppData.getContext()  
  12.                 .getSharedPreferences(USER_INFO_TAG, Context.MODE_PRIVATE);  
  13.         boolean change = sharedPreferences.getBoolean(SPLASH_TAG, false);  
  14.         return change;  
  15.     }  
  16.       
  17.     public static String getSplashImagePath() {  
  18.         SharedPreferences sharedPreferences = AppData.getContext()  
  19.                 .getSharedPreferences(USER_INFO_TAG, Context.MODE_PRIVATE);  
  20.         String path = sharedPreferences.getString(FILE_TAG, "0");  
  21.         return path;  
  22.     }  

4、最后一步就是在启动画面的时候,判断是否有新的启动图片,有的话,显示新的启动图片:


[java] view plaincopy
  1. if (UserInfo.isSplashImageChange()) {  
  2.             String file = UserInfo.getSplashImagePath();  
  3.             F.makeLog(file.toString());  
  4. //          F.makeToast(file.toString());  
  5.             Bitmap bitmap = BitmapFactory.decodeFile(file);  
  6.             imageView.setImageBitmap(bitmap);  
  7.         }  

这样就可以实现更新启动图片了。

由于是上班时间,偷偷来更新博客的,写的很是简略,不过应该可以看懂的吧,代码是集成在公司的项目中,不好上源代码,望海涵。



0 0