2011-09-25 49 views
1

更新即使我沒有從緩存中檢索圖像,我也試圖通過Drawable檢索,其中我將所有18個圖像存儲在「drawable-mdpi」夾。仍然顯示一個空白屏幕。使用Android中的BitmapFactory.decodeStream()從緩存中加載圖像

我能夠從服務器檢索圖像並將圖像(.GIF)保存到緩存中。但是,當我需要從緩存中加載該圖像時,圖像不會顯示在屏幕上。下面是做的工作代碼:

File cacheDir = context.getCacheDir(); 

      File cacheMap = new File(cacheDir, smallMapImageNames.get(i).toString()); 
      if(cacheMap.exists()){ 
       FileInputStream fis = null; 
       try { 
        fis = new FileInputStream(cacheMap); 
        Bitmap local = BitmapFactory.decodeStream(fis); 
        puzzle.add(local); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } 

      }else{ 
       Drawable smallMap = LoadImageFromWebOperations(mapPiecesURL.get(i).toString()); 
       if(i==0){ 
        height1 = smallMap.getIntrinsicHeight(); 
        width1 = smallMap.getIntrinsicWidth(); 
       } 
       if (smallMap instanceof BitmapDrawable) { 
        Bitmap bitmap = ((BitmapDrawable)smallMap).getBitmap(); 
        FileOutputStream fos = null; 
        try { 
         cacheMap.createNewFile(); 
         fos = new FileOutputStream(cacheMap); 
         bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); 
         fos.flush();  
         fos.close(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        }  

        puzzle.add(bitmap); 
       } 
      } 

ArrayList中存儲的圖片名稱:smallMapImageNames(圖像名稱也可以在網址中找到)

ArrayList中存儲的圖像的URL :mapPiecesURL

總結它我有2個問題 1)如何從緩存中加載圖像? 2)關於bitmap.compress(),來自服務器的圖像是.GIF格式,但我應用Bitmap.CompressFormat.PNG。那麼這會有什麼問題嗎?

任何人都可以幫助我嗎?

兩個功能

private Bitmap getBitMap(Context context) { 
     // TODO Auto-generated method stub 
     WifiPositioningServices wifiPositioningServices = new WifiPositioningServices(); 

     String[] mapURLandCalibratedPoint1 = wifiPositioningServices.GetMapURLandCalibratedPoint("ERLab-1_1.GIF","ERLab"); //list of map pieces url in the first 9 pieces 
     String[] mapURLandCalibratedPoint2 = wifiPositioningServices.GetMapURLandCalibratedPoint("ERLab-4_1.GIF","ERLab"); //list of map pieces url in the last 9 pieces 
     ArrayList<String> smallMapImageNames = new ArrayList<String>(); 
     ArrayList<String> mapPiecesURL = new ArrayList<String>(); 

     for(int i=0; i<mapURLandCalibratedPoint1.length; i++){ 
       if(mapURLandCalibratedPoint1[i].length()>40){ //image url 
        int len = mapURLandCalibratedPoint1[i].length(); 
        int subStrLen = len-13; 
        smallMapImageNames.add(mapURLandCalibratedPoint1[i].substring(subStrLen, len-3)+"JPEG"); 
        mapPiecesURL.add(mapURLandCalibratedPoint1[i]); 
       } 
       else{ 
        //perform other task 
       } 

     } 

     for(int i=0; i<mapURLandCalibratedPoint2.length; i++){ 
      if(mapURLandCalibratedPoint2[i].length()>40){ //image url 
       int len = mapURLandCalibratedPoint2[i].length(); 
       int subStrLen = len-13; 
       smallMapImageNames.add(mapURLandCalibratedPoint2[i].substring(subStrLen, len-3)+"JPEG"); 
       mapPiecesURL.add(mapURLandCalibratedPoint2[i]); 
      } 
      else{ 
       //perform other task 
      }  
     } 
     Bitmap result = Bitmap.createBitmap(1029, 617, Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(result); 
     ArrayList<Bitmap> puzzle = new ArrayList<Bitmap>(); 

     int height1 = 0 ; 
     int width1 = 0; 

     File cacheDir = context.getCacheDir(); 

     for(int i=0; i<18; i++){     
      File cacheMap = new File(cacheDir, smallMapImageNames.get(i).toString()); 
      if(cacheMap.exists()){ 
       //retrieved from cached 
       try {   
        FileInputStream fis = new FileInputStream(cacheMap);     
        Bitmap bitmap = BitmapFactory.decodeStream(fis); 
        puzzle.add(bitmap); 
       } catch (FileNotFoundException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      }else{ 
       //retrieve from server and cached it 
       Drawable smallMap = LoadImageFromWebOperations(mapPiecesURL.get(i).toString()); 
       if(i==0){ 
        height1 = smallMap.getIntrinsicHeight(); 
        width1 = smallMap.getIntrinsicWidth(); 
       } 
       if (smallMap instanceof BitmapDrawable) { 
        Bitmap bitmap = ((BitmapDrawable)smallMap).getBitmap(); 
        FileOutputStream fos = null; 
        try { 
         cacheMap.createNewFile(); 
         fos = new FileOutputStream(cacheMap); 
         bitmap.compress(CompressFormat.JPEG, 100, fos); 
         fos.flush();  
         fos.close(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        }  

        puzzle.add(bitmap); 
       } 
      }  
     } 

     Rect srcRect; 
     Rect dstRect; 
     int cnt =0; 


     for (int j = 0; j < 3; j++) { 
      int newHeight = height1 * (j % 3); 
      for (int k = 0; k < 3; k++) { 
       if (j == 0 && k == 0) { 
        srcRect = new Rect(0, 0, width1, height1); 
        dstRect = new Rect(srcRect); 
       } else { 
        int newWidth = width1 * k; 
        srcRect = new Rect(0, 0, width1, height1); 
        dstRect = new Rect(srcRect); 
        dstRect.offset(newWidth, newHeight); 
       } 
       canvas.drawBitmap(puzzle.get(cnt), srcRect, dstRect,null); 
       cnt++; 
      } 
     } 

     for(int a=0; a<3; a++){ 
      int newHeight = height1 * (a % 3); 
      for (int k = 3; k < 6; k++) { 
       if (a == 0 && k == 0) { 
        srcRect = new Rect(0, 0, width1*3, height1); 
        dstRect = new Rect(srcRect); 
       } else { 
        int newWidth = width1 * k; 
        srcRect = new Rect(0, 0, width1, height1); 
        dstRect = new Rect(srcRect); 
        dstRect.offset(newWidth, newHeight); 
       } 
       canvas.drawBitmap(puzzle.get(cnt), srcRect, dstRect, 
         null); 
       cnt++; 
      } 
     } 
     return result; 
    } 

    private Drawable LoadImageFromWebOperations(String url) { 
     // TODO Auto-generated method stub 
     try 
     { 
      InputStream is = (InputStream) new URL(url).getContent(); 
      Drawable d = Drawable.createFromStream(is, "src name"); 
      return d; 
     }catch (Exception e) { 
      System.out.println("Exc="+e); 
      return null; 
     } 
    } 

我實際上試圖顯示圖像的18個(3X6),以形成一個平面佈置圖。所以要顯示圖像,我使用兩個for-loop來顯示它。兩個.GIF圖像,ERLab-1_1.GIF和ERLab-4_1.GIF是每個組的中心部分。例如,第一行將是ERLab-0_0.GIF,ERLab-1_0.GIF,ERLab-2_0.GIF,ERLab-3_0.GIF,ERLab-4_0.GIF,ERLab-5_0.GIF。第二行將是第三行的XXX-X_1.GIF和XXX-X_2.GIF。

最後,

Bitmap resultMap = getBitMap(this.getContext()); 
bmLargeImage = Bitmap.createBitmap(1029 , 617, Bitmap.Config.ARGB_8888); 
bmLargeImage = resultMap; 
在OnDraw函數

然後將圖像繪製被在畫布上。

+0

什麼是拼圖? – Blackbelt

+0

對不起,我忘了提這個。它只是另一個ArrayList,用於在從緩存(如果緩存中已存在圖像)或從服務器中獲取圖像後存儲所有位圖圖像。 – user918197

+0

'decodeStream'成功返回一個位圖? – Ronnie

回答

2

我剛剛解決了我自己的問題。

在這一行,canvas.drawBitmap(puzzle.get(cnt), srcRect, dstRect,null);在我用它來繪製位圖到畫布上的每個for循環中,我需要將ArrayList(拼圖)中的每個項目投射到位圖。只有這樣才能顯示圖像。

我認爲如果ArrayList是這樣定義的,ArrayList<Bitmap> puzzle = new ArrayList<Bitmap>(); ArrayList中的每個項目都是位圖類型。但是這並不總是這樣嗎?