对于文件的操作:
保存到本地:
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;
}