2013-06-13 106 views
4

我不明白爲什麼我得到這個IO異常。它說,在文檔"Throws IOException if the image format is not supported or can not be decoded.""he stream's position will be where ever it was after the encoded data was read. Currently only the JPEG and PNG formats are supported.",我用bitmapOptions.outMimeType;給它一個PNG,我甚至輸出的圖像類型和它說image/pngOdd BitmapRegionDecoder java.io.IOException:圖像格式不支持w/png和jpeg圖像

代碼:

@Override 
protected List<String> doInBackground(String... urlo) { 
    List<String> savedKeys = new ArrayList<String>(); 
    InputStream inputStream = null; 
    try { 
     URL url = new URL("https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png"); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      inputStream = connection.getInputStream(); 
    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 



    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); 
    bitmapOptions.inJustDecodeBounds = true; 
    BitmapFactory.decodeStream(inputStream, null, bitmapOptions); 
    int width = bitmapOptions.outWidth; 
    int height = bitmapOptions.outHeight; 
    String imageType = bitmapOptions.outMimeType; 
    Debug.out(imageType); 



    int block = 256; 
    int count =0; 
    BitmapRegionDecoder decoder = null; 
    try { 
     decoder = BitmapRegionDecoder. 
      newInstance(inputStream, false); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    Rect tileBounds = new Rect(); 
    // loop blocks 
    for (int i=0; i<height; i+=block) { 
     // get vertical bounds limited by image height 
     tileBounds.top = i; 
     int h = i+block<height ? block : height-i; 
     tileBounds.bottom = i+h; 
     tileBounds.right = 0; 
     tileBounds.left = width; 
     Debug.out(tileBounds.left+" "+tileBounds.right+" "+tileBounds.top+tileBounds.bottom); 
      // load tile 
     tile = decoder.decodeRegion(tileBounds, bitmapOptions); 
     MainActivity.cache.put(key+count, tile); 
      publishProgress(); 
      tile.recycle(); 
     savedKeys.add(key+count); 

    } 
    try { 
     inputStream.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return savedKeys; 

} 

回答

1

的exeption不拋出因錯誤的文件類型,但因爲它不可讀。

BitmapFactory.decodeStream(inputStream, null, bitmapOptions); 

這個「遍歷」流,並且你的流對象不再從頭開始。

解決方法是在此之後再次調用inputStream = connection.getInputStream();,以使流返回到它的可讀狀態。

相關問題