android圖片快取實現(自定義ImageLoader)
阿新 • • 發佈:2019-02-07
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.BitmapFactory; import android.os.Environment; public class FileUtils { /** * sd卡的根目錄 */ private static String mSdRootPath = Environment.getExternalStorageDirectory().getPath(); /** * 手機的快取根目錄 */ private static String mDataRootPath = null; /** * 儲存Image的目錄名 */ private final static String FOLDER_NAME = "/tea/image"; public FileUtils(Context context) { mDataRootPath = context.getCacheDir().getPath(); } /** * 獲取儲存Image的目錄 * * @return */ private String getStorageDirectory() { return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ? mSdRootPath + FOLDER_NAME : mDataRootPath + FOLDER_NAME; } /** * 儲存Image的方法,有sd卡儲存到sd卡,沒有就儲存到手機目錄 * * @param fileName * @param bitmap * @throws IOException */ public void savaBitmap(String url, Bitmap bitmap) throws IOException { if (bitmap == null) { return; } String path = getStorageDirectory(); File folderFile = new File(path); if (!folderFile.exists()) { folderFile.mkdirs(); } File file = new File(path + File.separator + getFileName(url)); file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); bitmap.compress(CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } /** * 從手機或者sd卡獲取Bitmap * * @param fileName * @return */ public Bitmap getBitmap(String url) { return BitmapFactory.decodeFile(getStorageDirectory() + File.separator + getFileName(url)); } /** * 判斷檔案是否存在 * * @param fileName * @return */ public boolean isFileExists(String fileName) { return new File(getStorageDirectory() + File.separator + getFileName(fileName)).exists(); } /** * 獲取檔案的大小 * * @param fileName * @return */ public long getFileSize(String url) { return new File(getStorageDirectory() + File.separator + getFileName(url)).length(); } /** * 刪除SD卡或者手機的快取圖片和目錄 */ public void deleteFile() { File dirFile = new File(getStorageDirectory()); if (!dirFile.exists()) { return; } if (dirFile.isDirectory()) { String[] children = dirFile.list(); for (int i = 0; i < children.length; i++) { new File(dirFile, children[i]).delete(); } } dirFile.delete(); } /** * * @Title: getFileName * @說 明: 根據url擷取檔名 * @參 數: @param url * @參 數: @return * @return String 返回型別 * @throws */ public String getFileName(String url) { return url.substring(url.lastIndexOf("/") + 1); } } -------------------------------------------下載工具類------------------------------------------------------------------- package com.qianfeng.android.utils; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.params.HttpClientParams; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.params.HttpProtocolParams; import org.apache.http.util.EntityUtils; import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class HttpUtils { /** * * @Title: getBitmapFormUrl * @說 明:從伺服器獲取Bitmap * @參 數: @param url * @參 數: @return * @return Bitmap 返回型別 * @throws */ public static Bitmap getBitmapFormUrl(String url) { Bitmap bitmap = null; HttpClient httpClient = new DefaultHttpClient(); // 設定超時時間 HttpConnectionParams.setConnectionTimeout(new BasicHttpParams(), 6 * 1000); HttpGet get = new HttpGet(url); try { HttpResponse response = httpClient.execute(get); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); bitmap = BitmapFactory.decodeStream(entity.getContent()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return bitmap; } public static String getJsonFromServer(String url) { HttpClient httpClient = getHttpClient(); HttpGet get = new HttpGet(url); try { HttpResponse response = httpClient.execute(get); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); return EntityUtils.toString(entity); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (get != null) { get.abort(); } } return null; } /** * * @Title: getHttpClient * @說 明:設定httpClient的一些引數 * @參 數: @return * @return HttpClient 返回型別 * @throws */ private static HttpClient getHttpClient() { // 建立 HttpParams 以用來設定 HTTP 引數(這一部分不是必需的) HttpParams httpParams = new BasicHttpParams(); // 設定連線超時和 Socket 超時,以及 Socket 快取大小 HttpConnectionParams.setConnectionTimeout(httpParams, 20 * 1000); HttpConnectionParams.setSoTimeout(httpParams, 20 * 1000); HttpConnectionParams.setSocketBufferSize(httpParams, 8192); // 設定重定向,預設為 true HttpClientParams.setRedirecting(httpParams, true); // 設定 user agent String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2) Gecko/20100115 Firefox/3.6"; HttpProtocolParams.setUserAgent(httpParams, userAgent); // 建立一個 HttpClient 例項 // 注意 HttpClient httpClient = new HttpClient(); 是Commons HttpClient // 中的用法,在 Android 1.5 中我們需要使用 Apache 的預設實現 DefaultHttpClient HttpClient httpClient = new DefaultHttpClient(httpParams); return httpClient; } }