Android资源存取工具类

/**
 * 资源管理类
 * @author: linxcool.hu
 */
public class ResourceManager {
	private static final String TAG="ResourceManager";
	private static ResourceManager resourceManager=new ResourceManager();
	private List<Bitmap> bitmapCaches;
	
	private ResourceManager(){
		bitmapCaches=new ArrayList<Bitmap>();
	}
	public static ResourceManager getInstance(){
		return resourceManager;
	}
	/**
	 * 检查SD卡是否存在
	 * @return 存在返回true,否则返回false
	 */
	public boolean isSdcardReady(){
		boolean sdCardExist = Environment.getExternalStorageState().equals(
					android.os.Environment.MEDIA_MOUNTED);
		return sdCardExist;
	}
	/**
	 * 获得SD路径
	 * @return
	 */
	public String getSdcardPath() {
		return Environment.getExternalStorageDirectory().toString()+ File.separator;
	}
	
	/**
	 * 获取手机该应用的私有路径
	 * @param context
	 * @return
	 */
	public String getRomCachePath(Context context){
		return context.getCacheDir() + File.separator;
	}
	
	/**
	 * 检查SD卡中是否存在该文件
	 * @param filePath 不包含SD卡目录的文件路径
	 * @return
	 */
	public boolean isSdcardFileExist(String filePath){
		File file=new File(getSdcardPath()+filePath);
		return file.exists();
	}
	
	/**
	 *  检查手机内存中是否存在该文件
	 * @param context
	 * @param fileName 不包含应用内存目录的文件路径
	 * @return
	 */
	public boolean isRomCacheFileExist(Context context,String filePath){
		String cachePath = getRomCachePath(context);
		File file=new File(cachePath+filePath);
		return file.exists();
	}
	
	/**
	 * 构建SD目录
	 * @param path 不包含SD卡目录的文件全路径
	 * @return
	 */
	public String mkSdcardFileDirs(String path) {
		String rsPath =getSdcardPath() + path;
		File file = new File(rsPath);
		if (!file.exists())file.mkdirs();
		return rsPath;
	}
	
	/**
	 * 构建手机存储文件路径
	 * @param context
	 * @param path 不包含应用内存目录的文件全路径
	 * @return
	 */
	public String mkRomCacheDirs(Context context,String path) {
		String cachePath = getRomCachePath(context);
		String rsPath=cachePath+path;
		File file = new File(rsPath);
		if (!file.exists())file.mkdirs();
		return rsPath;
	}
	
	/**
	 * 保存图片文件
	 * @param bmp
	 * @param fullFilePath 保存的完整路径
	 * @return 返回true如果图片保存失败或图片已存在,否则返回false
	 */
	public boolean saveBitmap(Bitmap bmp,String fullFilePath) {
		if(fullFilePath==null||fullFilePath.length()<1){
			Log.e(TAG, "saveBitmap error as filePath invalid");
			return false;
		}
		FileOutputStream fos = null;
		File file = new File(fullFilePath);
		if(file.exists())return false;
		try {
			fos = new FileOutputStream(file);
			if (null != fos) {
				bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);
				fos.flush();
				fos.close();
				return true;
			}
		} catch (IOException e) {
			Log.e(TAG, "saveBitmap fail as "+e.getMessage());
		}
		return false;
	}
	
	/**
	 * 保存图片到SD卡
	 * @param bmp 图片文件
	 * @param filePath 
	 */
	public boolean saveBitmap2Sdcard(Bitmap bmp,String filePath) {
		if(!isSdcardReady()){
			Log.e(TAG, "saveBitmap2Sdcard error as sdCard not exist");
			return false;
		}
		return saveBitmap(bmp, getSdcardPath()+filePath);
	}
	
	/**
	 * 保存图片到手机内存
	 * @param context
	 * @param bmp 图片文件
	 * @param filePath 
	 * @return
	 */
	public boolean saveBitmap2RomCache(Context context,Bitmap bmp,String filePath){
		return saveBitmap(bmp, getRomCachePath(context)+filePath);
	}
	
	/**
	 * 获取图片资源
	 * @param fullFilePath
	 * @return
	 */
	public Bitmap getBitmap(String fullFilePath){
		try {
			File file=new File(fullFilePath);
			if(file.exists()){
				Bitmap bitmap = BitmapFactory.decodeFile(fullFilePath);
				bitmapCaches.add(bitmap);
				return bitmap;
			}
		} catch (Exception e) {
			Log.e(TAG, "getBitmap fail as "+e.getMessage());
		}
		return null;
	}
	
	/**
	 * 从SD卡获取图片资源
	 * @param fullFilePath
	 * @return
	 */
	public Bitmap getBitmapFromSdcard(String filePath){
		return getBitmap(getSdcardPath()+filePath);
	}
	
	/**
	 * 从手机内存获取图片资源
	 * @param fullFilePath
	 * @return
	 */
	public Bitmap getBitmapFromRomCache(Context context,String filePath){
		return getBitmap(getRomCachePath(context)+filePath);
	}
	
	/**
	 * 获得包路径下的图片文件
	 * @param pkgPath
	 * @param fileName
	 * @return
	 */
	public Bitmap getBitmapFromPackage(Class<?> clazz,String pkgPath,String fileName) {
		InputStream is = clazz.getResourceAsStream(pkgPath+fileName);
		try {
			if (null == is || is.available() <= 0)return null;
		} catch (IOException ioe) {
			Log.e(TAG, ioe.getMessage());
			try {
				if (is != null)is.close();
			}catch (Exception e) {}
			return null;
		}
		try {
			Bitmap bitmap=BitmapFactory.decodeStream(is);
			bitmapCaches.add(bitmap);
			return bitmap;
		}catch (Exception e) {
			Log.e(TAG, "getBitmapFromPackage fail as "+e.getMessage());
		}finally {
			try {
				if (is != null)is.close();
			}catch (Exception e) {}
		}
		return null;
	}
	
	/**
	 *  获得包路径下的.9.png格式图片
	 * @param clazz
	 * @param pkgPath
	 * @param picName
	 * @return
	 */
	public Drawable get9pngDrawableFromPackage(
			Class<?> clazz,String pkgPath, String picName) {
		InputStream inputStream = clazz.getResourceAsStream(pkgPath + picName);
		try {
			if (null == inputStream || inputStream.available() <= 0) 
				return null;
		} 
		catch (IOException e1) {
			try {
				if (inputStream != null) 
					inputStream.close();
			} 
			catch (Exception e) {}
			return null;
		}
		Bitmap bitmap = null;
		NinePatchDrawable patchy = null;
		try {
			bitmap = BitmapFactory.decodeStream(inputStream);
			bitmapCaches.add(bitmap);
			byte[] chunk = bitmap.getNinePatchChunk();
			boolean result = NinePatch.isNinePatchChunk(chunk);
			if (result) patchy = new NinePatchDrawable(bitmap, chunk, new Rect(), null);
		} 
		catch (Exception e) {} 
		finally {
			try {
				if (inputStream != null) 
					inputStream.close();
			} 
			catch (Exception e) {}
		}
		return patchy;
	}

	/**
	 * 释放引用的图片
	 */
	public void recycleBitmaps(){
		for (int i = 0; i < bitmapCaches.size(); i++) {
			bitmapCaches.get(i).recycle();
		}
		bitmapCaches.clear();
	}
	
	/**
	 * 从文件中读取字符串
	 * @param file
	 * @return
	 */
	public String readStringFile(File file){
		try {
			InputStream is = new FileInputStream(file);
			return readString(is);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 从文件中读取字符串
	 * @param file
	 * @return
	 */
	public String readStringFromPackage(Class<?> clazz,String pkgPath,String fileName){
		try {
			InputStream is=clazz.getResourceAsStream(pkgPath+fileName);
			return readString(is);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private String readString(InputStream is){
		ByteArrayOutputStream bos = null;
		try{
			bos = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int length = -1;
			while( (length = is.read(buffer)) != -1){
				bos.write(buffer,0,length);
			}
			return bos.toString(); 
		} catch(Exception e){
			Log.e(TAG, "readStringFromPackage fail as "+e.getMessage());
		}finally{
			try {bos.close();
			} catch (IOException e) {}
			try {is.close();
			} catch (IOException e) {}
		}
		return null;
	}
	
	/**
	 * 从SD卡读取字符文件
	 * @param filePath
	 * @return
	 */
	public String readStringFileFromSdcard(String filePath) {
		if(!isSdcardReady()){
			Log.e(TAG, "saveBitmap2Sdcard error as sdCard not exist");
			return null;
		}
		File file=new File(getSdcardPath()+filePath);
		return readStringFile(file);
	}
	
	/**
	 * 从手机内存读取字符文件
	 * @param filePath
	 * @return
	 */
	public String readStringFileFromRomCache(Context context,String filePath) {
		File file=new File(getRomCachePath(context)+filePath);
		return readStringFile(file);
	}
	
	/**
	 * 存储字符文件
	 * @param fullFilePath 完整路径
	 * @param content 字符内容
	 * @return
	 */
	public boolean saveStringFile(String fullFilePath,String content){
		File file = new File(fullFilePath);
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			fos.write(content.getBytes());
			return true;
		} catch (IOException e) {
			Log.e(TAG, "saveData fail as "+e.getMessage());
		}finally{
			try {
				if(fos!=null) fos.close();
			} catch (IOException e) {}
		}
		return false;
	}
	
	/**
	 *  存储字符文件到手机内存
	 * @param context
	 * @param fileName
	 * @param content
	 * @return
	 */
	public boolean saveStringFile2RomCache(Context context,String filePath,String content){
		String cachePath = getRomCachePath(context);
		return saveStringFile(cachePath+filePath, content);
	}
	
	/**
	 * 存储字符文件到SD卡
	 * @param filePath
	 * @param content
	 * @return
	 */
	public boolean saveStringFile2Sdcard(String filePath,String content) {
		if(!isSdcardReady()){
			Log.e(TAG, "saveStringFile2Sdcard error as sdCard not exist");
			return false;
		}
		return saveStringFile(getSdcardPath()+filePath,content);
	}
}


public class ResUtil {
	public static final String SD_IMG_PATH="istore/images/";
	public static final String ROM_IMG_PATH="images/";
	public static final String SD_DATA_PATH="istore/data/";
	public static final String ROM_DATA_PATH="data/";
	
	public static final String PKG_PATH = "/com/linxcool/istore/";
	public static final String PKG_RES_PATH = PKG_PATH+"res/";
	
	/**
	 * 获得包图片资源
	 * @param picName
	 * @return
	 */
	public static Bitmap getBitmapFromPackage(String picName){
		return ResourceManager.getInstance().getBitmapFromPackage(
				ResUtil.class,PKG_RES_PATH, picName);
	}
	
	/**
	 * 获得包.9.png图片资源
	 * @param picName
	 * @return
	 */
	public static Drawable get9pngDrawableFromPackage(String picName){
		return ResourceManager.getInstance().get9pngDrawableFromPackage(
				ResUtil.class, PKG_RES_PATH, picName);
	}
	
	/**
	 * 获得SD_APK_PATH路径下的APK文件
	 * @return
	 */
	public static List<File> getApkFiles(){
		File filesPath=new File(
				ResourceManager.getInstance().getSdcardPath()+SD_APK_PATH);
		if(!filesPath.exists())return null;
		File[] files=filesPath.listFiles();
		if(files==null || files.length==0)return null;
		
		List<File> list=new ArrayList<File>();
		for (File file : files) {
			if(file.isDirectory())continue;
			String fileName=file.getName().toLowerCase();
			if(fileName.contains(".apk"))
				list.add(file);
		}
		return list;
	}
	
	public static String getFolderFromPath(String path) {
		String[] slipArray = path.split(File.separator);
		if( slipArray.length <= 0 )
			return null;
		int pathLen = path.length();
		int fileNameLen = slipArray[slipArray.length-1].length();
		return path.substring(0, pathLen-fileNameLen);
	}
	
	public static String readStringFromPackage(String fileName){
		return ResourceManager.getInstance().readStringFromPackage(
				ResUtil.class,PKG_RES_PATH, fileName);
	}
	
	/**
	 * 保存String数据
	 * @param context
	 * @param fileName
	 * @param content
	 */
	public static void saveStringData(Context context,String fileName,String content){
		if(ResourceManager.getInstance().isSdcardReady()){
			String filePath=SD_DATA_PATH+fileName;
			String folder=getFolderFromPath(filePath);
			ResourceManager.getInstance().mkSdcardFileDirs(folder);
			ResourceManager.getInstance().
			saveStringFile2Sdcard(filePath, content);
		}else{
			String filePath=ROM_DATA_PATH+fileName;
			String folder=getFolderFromPath(filePath);
			ResourceManager.getInstance().mkRomCacheDirs(context, folder);
			ResourceManager.getInstance().
			saveStringFile2RomCache(context, filePath, content);
		}
	}
	
	/**
	 * 读取String数据
	 * @param context
	 * @param fileName
	 * @return
	 */
	public static String readStringData(Context context,String fileName){
		if(ResourceManager.getInstance().isSdcardFileExist(SD_DATA_PATH+fileName)){
			return ResourceManager.getInstance().
			readStringFileFromSdcard(SD_DATA_PATH+fileName);
		}else if(ResourceManager.getInstance().isRomCacheFileExist(context, ROM_DATA_PATH+fileName)){
			return ResourceManager.getInstance().
			readStringFileFromRomCache(context,ROM_DATA_PATH+fileName);
		}
		return null;
	}
	
	/**
	 * 保存图片文件
	 * @param context
	 * @param bmp
	 * @param fileName
	 */
	public static void saveBitmap(Context context,Bitmap bmp, String fileName){
		if(ResourceManager.getInstance().isSdcardReady()){
			String filePath=SD_IMG_PATH+fileName;
			String folder=getFolderFromPath(filePath);
			ResourceManager.getInstance().mkSdcardFileDirs(folder);
			ResourceManager.getInstance().saveBitmap2Sdcard(bmp,filePath);
		}else{
			String filePath=ROM_IMG_PATH+fileName;
			String folder=getFolderFromPath(filePath);
			ResourceManager.getInstance().mkRomCacheDirs(context, folder);
			ResourceManager.getInstance().
			saveBitmap2RomCache(context, bmp, filePath);
		}
	}
	
	/**
	 * 获得图片文件
	 * @param context
	 * @param fileName
	 * @return
	 */
	public static Bitmap getBitmap(Context context,String fileName){
		if(ResourceManager.getInstance().isSdcardFileExist(SD_IMG_PATH+fileName)){
			return ResourceManager.getInstance().
			getBitmapFromSdcard(SD_IMG_PATH+fileName);
		}else if(ResourceManager.getInstance().isRomCacheFileExist(context, ROM_IMG_PATH+fileName)){
			return ResourceManager.getInstance().
			getBitmapFromRomCache(context,ROM_IMG_PATH+fileName);
		}
		return null;
	}
	
}


  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值