最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android編程實(shí)現(xiàn)圖片庫(kù)的封裝方法

 更新時(shí)間:2015年11月02日 12:14:18   作者:年輕的zhangchang  
這篇文章主要介紹了android編程實(shí)現(xiàn)圖片庫(kù)的封裝方法,涉及Android針對(duì)圖片的下載、保存、獲取及操作緩存圖片等相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了android編程實(shí)現(xiàn)圖片庫(kù)的封裝方法。分享給大家供大家參考,具體如下:

大家在做安卓應(yīng)用的時(shí)候 經(jīng)常要從網(wǎng)絡(luò)中獲取圖片 都是通過(guò)URL去獲取 可是如果本地有圖片數(shù)據(jù) 從本地獲取數(shù)據(jù)不更加快一些 自己在工作中遇到這個(gè)問(wèn)題 所以采用了一個(gè)URL和本地圖片的一個(gè)映射關(guān)系 先從本地區(qū)獲取 假如本地沒(méi)有再?gòu)木W(wǎng)絡(luò)中獲取 本方法考慮到多線程問(wèn)題 歡迎大家一起共同探討!

public class PictureLibrary {
 /*
  * 圖片庫(kù)的操作
  */
 File file;
 URL url;
 HttpURLConnection conn;
 InputStream is;
 FileOutputStream fos;
 private Lock lock = new ReentrantLock();
 private Condition downFile = lock.newCondition();
 // 通過(guò)URL將數(shù)據(jù)下載到本地操作
 private String toLocalFile(String strURL) {
  String fileName = Utils.getFileName(strURL);
  String path = Environment.getExternalStorageDirectory() + "/"
    + EcologicalTourism.FILE_PATH + "/images/" + fileName;
  return path;
 }
 // 通過(guò)URL將數(shù)據(jù)下載到本地臨時(shí)文件中
 private String toLocalFileTemp(String strURL) {
  String s = Utils.getFileName(strURL);
  String fileName = s+"temp";
  String path_url = Environment.getExternalStorageDirectory() + "/"
    + EcologicalTourism.FILE_PATH + "/tempimages/" + fileName;
  return path_url;
 }
 /*
  * 保存圖片到本地,并返回本地url(此函數(shù)是阻塞的)
  * main
  * @參數(shù):strURL,參數(shù)為圖片的URL.返回值:該圖片在本地SD卡暫存的位置
  * 函數(shù)的工作是負(fù)責(zé)將圖片從互聯(lián)網(wǎng)上取得,存在本地存儲(chǔ)中,并返回本地存儲(chǔ)的文件路徑,供調(diào)用者直接使用。如果文件已經(jīng)存在本地,直接返回
  * 如果文件未在本地,則直接從服務(wù)器下載,函數(shù)阻塞。
  */
 public String getReadSD(String strURL) {
  Log.i("test", "拿到網(wǎng)絡(luò)的地址是:" + strURL);
  String strLocalFile = toLocalFile(strURL); //k:把服務(wù)器URL轉(zhuǎn)換為本地URL
  String strLocalFileTemp = toLocalFileTemp(strURL); //k:把服務(wù)器URL轉(zhuǎn)換為本地臨時(shí)URL
  while (true) {
   File file = new File(strLocalFile);
   Log.i("test", "本地文件是:" + strLocalFile);
   File tfile = new File(strLocalFileTemp);
   Log.i("test", "臨時(shí)文件是:" + strLocalFileTemp);
   // 1上鎖
   lock.lock();
   if (file.exists()) {
    // 2if 本地文件存在
    // 解鎖
    // 返回本地路徑
    lock.unlock();
    Log.i("test", "返回本地路徑:" + file);
    return strLocalFile;
   } else if (tfile.exists()) {
    // if 對(duì)應(yīng)的暫存文件存在
    // 解鎖
    lock.unlock();
    try {
     // 睡眠
     downFile.await();
    } catch (Exception e) {
      e.printStackTrace();
     Log.i("test", "e 出現(xiàn)了異常1" + e);
    }
   } else {
    try {
     // 創(chuàng)建對(duì)應(yīng)的暫存文件
     tfile.createNewFile();
    } catch (IOException e) {
     Log.i("test", "e 出現(xiàn)了異常2" + e);
    }
    // 解鎖
    lock.unlock();
    // 下載文件內(nèi)容到暫存文件中
    downURL2(strURL, strLocalFile);
    // 上鎖
    lock.lock();
    // 修改暫存文件名字為本地文件名
    tfile.renameTo(file);
    // 解鎖
    lock.unlock();
   }
  }
 }
 private void downURL2(String strURL, String strLocalFileTemp) {
  // TODO Auto-generated method stub
  URL url;
  try {
   url = new URL(strURL);
   HttpURLConnection conn = (HttpURLConnection) url.openConnection();
   conn.setConnectTimeout(5000);
   conn.setRequestMethod("GET");
   conn.setDoInput(true);
   if (conn.getResponseCode() == 200) {
     InputStream is = conn.getInputStream();
     FileOutputStream fos = new FileOutputStream(strLocalFileTemp);
     byte[] buffer = new byte[1024];
     int len = 0;
     while ((len = is.read(buffer)) != -1) {
       fos.write(buffer, 0, len);
     }
     is.close();
     fos.close();
     // 返回一個(gè)URI對(duì)象
   }
  } catch (MalformedURLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ProtocolException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 /*
  * 阻塞式下載url到文件 toFile中
  */
 private boolean downURL(String strURL, String toFile) {
  URL url;
  try {
   url = new URL(strURL);
   HttpURLConnection httpUrl = (HttpURLConnection) url
     .openConnection();
   httpUrl.setRequestMethod("GET");
   int fileSize = httpUrl.getContentLength();// 文件大小
   httpUrl.disconnect();// 關(guān)閉連接
   int threadSize = 6;// 默認(rèn)設(shè)置6個(gè)線程
   threadSize = fileSize % threadSize == 0 ? threadSize
     : threadSize + 1;
   int currentSize = fileSize / threadSize; // 每條線程下載大小
   String dowloadir = Environment.getExternalStorageDirectory() + "/"
     + EcologicalTourism.FILE_PATH + "/images/";
   File dir = new File(dowloadir);
   if (!dir.exists()) {
    dir.mkdirs();
   }
   File file = new File(dir, toFile);
   RandomAccessFile randomFile = new RandomAccessFile(file, "rw");
   randomFile.setLength(fileSize);// 指定 file 文件的大小
   for (int i = 0; i < threadSize; i++) {
    int startposition = i * currentSize;// 每條線程開(kāi)始寫(xiě)入文件的位置
    RandomAccessFile threadFile = new RandomAccessFile(file, "rw");
    Log.i("syso", "toFile的內(nèi)容是:" + toFile);
    threadFile.seek(startposition);
    new DownLoadThread(i, currentSize, threadFile, startposition,
      url).start();
   }
  } catch (Exception e) {
   e.printStackTrace();
   Log.i("syso", "download下載失敗" + e);
  }
  return true;
 }
 /**
  * 實(shí)現(xiàn)線程下載
  * 
  */
 private static class DownLoadThread extends Thread {
  @SuppressWarnings("unused")
  private int threadId;// 線程編號(hào)
  private int currentSize;// 每條線程的大小
  private RandomAccessFile threadFile; // 每條線程 要寫(xiě)入文件類(lèi)
  private int startposition;// 每條線程開(kāi)始寫(xiě)入文件的位置
  private URL url; //網(wǎng)絡(luò)地址
  public DownLoadThread(int threadId, int currentSize,
    RandomAccessFile threadFile, int startposition, URL url) {
   this.threadId = threadId;
   this.currentSize = currentSize;
   this.threadFile = threadFile;
   this.startposition = startposition;
   this.url = url;
  }
  public void run() {
   try {
    HttpURLConnection httpUrl = (HttpURLConnection) url
      .openConnection();
    httpUrl.setRequestMethod("GET");
    httpUrl.setRequestProperty("range", "bytes=" + startposition
      + "-");// 指定服務(wù)器的位置
    InputStream is = httpUrl.getInputStream();
    byte[] data = new byte[1024];
    int len = -1;
    int threadFileSize = 0;
    while ((threadFileSize < currentSize)
      && ((len = is.read(data)) != -1)) {
     threadFile.write(data, 0, len);
     threadFileSize += len;
    }
    httpUrl.disconnect();
    is.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 }
 /**
 * 從本緩存中獲取圖片
 */
 public Bitmap getBitmapFromCache(String imageURL) {
 // String bitmapName = imageURL.substring(imageURL.lastIndexOf("/") + 1); 
  String bitmapName = Utils.getFileName(imageURL);
  File cacheDir = new File(Environment.getExternalStorageDirectory() + "/"
    + EcologicalTourism.FILE_PATH + "/images/");
  File[] cacheFiles = cacheDir.listFiles();
  int i = 0;
  if(null!=cacheFiles){
   for(; i<cacheFiles.length;i++){
    if(bitmapName.equals(cacheFiles[i].getName())){
     break;
    }
   }
   if(i < cacheFiles.length)
   {
    return BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/"
      + EcologicalTourism.FILE_PATH + "/images/" + bitmapName);
   }
  }
  return null;
 }

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

吉林市| 延寿县| 平陆县| 澜沧| 鞍山市| 海口市| 林西县| 富宁县| 云浮市| 平谷区| 湖北省| 江油市| 阿鲁科尔沁旗| 贞丰县| 军事| 日喀则市| 达孜县| 广平县| 且末县| 浦江县| 无锡市| 定安县| 台安县| 施甸县| 平泉县| 光山县| 青龙| 招远市| 喀什市| 隆化县| 泗阳县| 搜索| 清新县| 永宁县| 商水县| 全州县| 彰武县| 南木林县| 横山县| 保德县| 江阴市|