本地保存gif图片,实现第二次不网络加载

对于文件的操作:

保存到本地:

public void saveGifFile(byte[] buffer, String gifFile, String fileName) {
		String ALBUM_PATH = gifFile + "/gif_images/";

		File dirFile = new File(ALBUM_PATH);
		if (!dirFile.exists()) {
			dirFile.mkdir();
		}
		File myCaptureFile = new File(ALBUM_PATH + fileName);
		Log.i(TAG, myCaptureFile.getPath());
		BufferedOutputStream bos;
		try {
			bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
			bos.write(buffer);
			bos.flush();
			bos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
这个是基于之前的显示gif的方法:http://blog.csdn.net/xunfan/article/details/42041935

     public void onSuccess(int arg0, Header[] arg1, byte[] arg2) { 
返回的参数是byte数组。

读取:

	public String getGifFile(String gifFile, String fileName) {
		String ALBUM_PATH = gifFile + "/gif_images/";
		try {
			File file = new File(ALBUM_PATH + fileName);
			if (file.exists()) {
				return ALBUM_PATH + fileName;
			} else {
				return "";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}

	}

若在磁盘有缓存,则返回缓存目录,否则返回“”;

然后直接new一个gifdrawable,并设置imageview的大小。

GifDrawable gifDrawable = new GifDrawable(str);
					android.view.ViewGroup.LayoutParams para;
					para = holder.gif_view.getLayoutParams();
					int width = getScreenWidth(mContext); // 屏幕宽度(像素)
					// 设置
					para.height = width * gifDrawable.getIntrinsicHeight()
							/ gifDrawable.getIntrinsicWidth();
					para.width = width;
					holder.gif_view.setLayoutParams(para);
					holder.gif_view.setBackgroundDrawable(gifDrawable);

使用到的函数:

// 获取屏幕的宽度
	public int getScreenWidth(Context context) {
		DisplayMetrics metric = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay()
				.getMetrics(metric);
		int width = metric.widthPixels; // 屏幕宽度(像素)
		return width;
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值