Android CacheImage之下载获取指定大小图片

public final class ImageUtils {
	private static final String TAG = "ImageUtils";
	private static final boolean DEBUG = false;
	/**
	 * 图片工具类
	 */
	
	private ImageUtils() {
	}
	
	
	/**
	 * 把图片转换成指定的大小,规则如下:
	 *       当前图片的宽度和高度是否都大于指定高度的二倍,
	 *       是则缩小2倍,继续判断大小
	 * @param f
	 * @param width
	 * @param height
	 * @return
	 * @throws IOException 
	 */
	public static Bitmap decode2file(File f,final int width,final int height) throws IOException {
		if(f == null){
			if(DEBUG)
				Log.i(TAG, "file is null in decode");
			return null;
		}
		if (!f.exists()) {
			throw new IOException("local file does not exist: " + f);
		}
		if (!f.canRead()) {
			throw new IOException("cannot read from local file: " + f);
		}
		if(DEBUG)
			Log.i(TAG, ">>>>>>>>>>>>>>decode start file name : " + f.getName() + ">> width : " + width + ">>height : " + height);
		try {
			// 加载图片,获取大小
			BitmapFactory.Options oc = new BitmapFactory.Options();
			// 设置成 true,只获取图片大小,不会把图片加载到内存中
			oc.inJustDecodeBounds = true;
			FileInputStream fisc = new FileInputStream(f);
			BitmapFactory.decodeStream(fisc, null, oc);
			fisc.close();
			fisc = null;
			
			//计算
			int tempw = oc.outWidth, temph = oc.outHeight;
			int scale = 1;
			while(true){
				if(tempw / 2 < width || temph < height)
					break;
				tempw /= 2;
				temph /= 2;
				scale *= 2;
			}
			//加载指定规格图片
			BitmapFactory.Options oi = new BitmapFactory.Options();
			oi.inSampleSize = scale;
			FileInputStream fisi = new FileInputStream(f);
			Bitmap b = BitmapFactory.decodeStream(fisi, null, oi);
			fisi.close();
			fisi = null;
			if(DEBUG)
				Log.i(TAG, "<<<<<<<<<<<<<<<<<< decode end");
			return b;
			
		} catch (FileNotFoundException e) {
			if(DEBUG)
				Log.e(TAG, "<<<<<<<<<<<<<<not find decode file");
		} catch (IOException e) {
			if(DEBUG)
				Log.e(TAG, "<<<<<<<<<<<<<io exception in decode");
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 下载指定大小的图片
	 * @param url
	 * @param width
	 * @param height
	 * @return
	 */
	public static  Bitmap getBitmapFromNet(String url,final int width,final int height){
		if(url == null){
			if (DEBUG)
				Log.i(TAG, ">>>>>>>>>>>>download image url is null");
			return null;
		}
		checkNotOnMainThead();
		if(DEBUG)
			Log.i(TAG, ">>>>>>>>>>>download image start url: " + url);
		
		Bitmap b = null;
		File tempFile = null;
		InputStream is = null;
		FileOutputStream fos = null;
		HttpURLConnection conn = null;
		try{
			URL imageURL = new URL(url);
			conn = (HttpURLConnection) imageURL.openConnection();
			//连接时间,等待读取时间和重定向
			conn.setConnectTimeout(20000);
			conn.setReadTimeout(20000);
			conn.setInstanceFollowRedirects(true);
			is = conn.getInputStream();
			tempFile = new File("temp_" + MD5.encode(url));
			fos = new FileOutputStream(tempFile);
			copyStream(is, fos);
			b = decode2file(tempFile, width, height);
			
			return b;
		}catch(Throwable tx){
			if(DEBUG)
				Log.e(TAG, "<<<<<<<<download exception");
			tx.printStackTrace();
			//发生内存溢出,清理缓存
			if(tx instanceof OutOfMemoryError){
				if(DEBUG)
					Log.e(TAG, "<<<<<<<<<<<download out of memory");
			}
			
		}finally{
			try {
				if (fos != null)
					fos.close();
				if(is != null)
					is.close();
				conn.disconnect();
				tempFile.delete();
			} catch (IOException e) {
				e.printStackTrace();
 
			}
		}
		return null;
	}
	
	/**
	 * 
	 * @param is
	 * @param os
	 * @throws IOException
	 */
	public static void copyStream(InputStream is , OutputStream os) throws IOException{
		final int buffSize = 1024;
		int count = 0;
		byte[] b = new byte[2 * buffSize];
		while(-1 != (count = is.read(b))){
			os.write(b, 0, count);
		}
	}
		/**
	 * 检查是否在主线程
	 */
	public static void checkNotOnMainThead(){
		if(Looper.myLooper() == Looper.getMainLooper()){
			throw new IllegalStateException("This method should not be called from the main/UI thread.");
		}
	}
	
	/**
	 * 
	 * @param b
	 * @param f
	 * @throws IOException
	 */
	public static void writeBitmap2File(Bitmap b, File f) throws IOException{
		if(b == null || f == null){
			if(DEBUG)
				Log.e(TAG, "bitmap or file is null bitmap: " + b + ">>>>>file: "  + f);
			return ;
		}
		if (!f.exists()) {
			throw new IOException("local file does not exist: " + f);
		}
		FileOutputStream fos = null;
		try{
			fos = new FileOutputStream(f);
			b.compress(CompressFormat .PNG, 100, fos);
			fos.flush();
		}catch(Exception ex){
			if(DEBUG)
				Log.e(TAG, "bitmap write to file exception");
		}finally{
			if(fos!=null)
			{
				try {
					fos.close();
				} catch (Exception e) {
					
				}
			}
		}
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android编译命令包括以下几个常用命令: 1. make clean: 执行清理操作,等同于 rm -rf out/,用于清理之前的编译结果。 2. make sdk: 编译Android的SDK,生成Android SDK的相关文件。 3. make update-api: 更新API,用于在framework API发生变动后更新API文件,API文件位于frameworks/base/api目录下。 4. make dist: 执行Build,并将输出文件拷贝到/out/dist目录,用于生成构建的输出文件。 5. make all: 编译所有内容,不管当前产品定义中是否包含。 6. make help: 显示帮助信息,列出主要的make目标。 7. make <local_target>: 编译指定的模块,<local_target>为模块的名称。 8. make clean-<local_target>: 清理指定模块的编译结果。 9. make dump-products: 显示所有产品的编译配置信息,包括产品名、支持的地区语言、包含的模块等。 10. make PRODUCT-xxx-yyy: 编译指定的产品,PRODUCT-xxx-yyy为产品的名称。 11. make bootimage: 生成boot.img,用于制作系统启动镜像。 12. make recoveryimage: 生成recovery.img,用于制作系统恢复镜像。 13. make userdataimage: 生成userdata.img,包含用户数据的镜像。 14. make cacheimage: 生成cache.img,缓存分区的镜像。 这些命令可以帮助进行Android的编译和构建工作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Android编译命令](https://blog.csdn.net/zhanghao19960914/article/details/129070496)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [i.mx6 Android5.1.1 build解析](https://blog.csdn.net/weixin_30725467/article/details/94764116)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值