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


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

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


View tempView = mGridView.getChildAt(0);
		ImageView imageView = (ImageView) tempView
				.findViewById(R.id.gv_image_detail);
		File file = ImageUtils.saveSplashImageToSdCard(imageView);

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


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;
		}
    }

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

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;
	}

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


if (UserInfo.isSplashImageChange()) {
			String file = UserInfo.getSplashImagePath();
			F.makeLog(file.toString());
//			F.makeToast(file.toString());
			Bitmap bitmap = BitmapFactory.decodeFile(file);
			imageView.setImageBitmap(bitmap);
		}

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

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


你可能感兴趣的:(android,bitmap,imageview)