2011-09-25 48 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中的每个项目都是位图类型。但是这并不总是这样吗?