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

来源:互联网 发布:阿里云服务器好卡 编辑:程序博客网 时间:2024/06/05 00:50

对于文件的操作:

保存到本地:

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



0 0
原创粉丝点击