2012-06-26 97 views
4

我需要获取在输入流中找到的图像的高度和宽度。下面是我做的:获取图像输入流的大小

private Boolean testSize(InputStream inputStream){ 
     BitmapFactory.Options Bitmp_Options = new BitmapFactory.Options(); 
     Bitmp_Options.inJustDecodeBounds = true; 
     BitmapFactory.decodeResourceStream(getResources(), new TypedValue(), inputStream, new Rect(), Bitmp_Options); 
     int currentImageHeight = Bitmp_Options.outHeight; 
     int currentImageWidth = Bitmp_Options.outWidth; 
     Bitmp_Options.inJustDecodeBounds = false; 
    if(currentImageHeight < 200 || currentImageWidth < 200){ 
     Object obj = map.remove(pageCounter); 
     Log.i("Page recycled", obj.toString()); 
     return true; 
    } 
    return false;} 

跳绳的问题点:

它改变BitmapFactory.Options即使我迫使它在我的下面第二种方法计算后假。

private Bitmap getBitmap(InputStream InpStream){ 
    Bitmap originalBitmap = BitmapFactory.decodeStream(InpStream);//Null. 
    return originalBitmap; 
} 

现在我的问题是有另一种方法从输入流获取图像的大小和宽度?我真的需要帮助,对此非常感谢。

   ZipInputStream zip = null; 
       zip = new ZipInputStream(new FileInputStream(getFileLocation())); 
       for(ZipEntry zip_e = zip.getNextEntry(); zip_e != null ; zip_e = zip.getNextEntry()){ 
        if(zip_e.isDirectory()) { 
         continue; 
        } 
        String file_zip = zip_e.getName(); 
        String comparison = map.get(pageCounter).getHref(); 
        if(file_zip.endsWith(comparison)){ 
         SpannableString Spanable_String = new SpannableString("abc"); 
         if(testSize(zip)){ 
          map.remove(pageCounter); 
          return false; 
         } 
         Bitmap bitmap = getBitmap(zip); 
         if(bitmap == null){ 
          map.remove(pageCounter); 
          return false; 
         } 
         image_page.put(zip_e.getName(), zip); 
         Drawable drawable_image = new FastBitmapDrawable(bitmap); 
         drawable_image.setBounds(0,0,drawable_image.getIntrinsicWidth(), drawable_image.getIntrinsicHeight()); 
         ImageSpan imageSpan = new ImageSpan(drawable_image, ImageSpan.ALIGN_BASELINE); 
         Spanable_String.setSpan(imageSpan, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
         tv.setText(Spanable_String); 
         return false; 
        } 
       } 
+0

你是说,将Bitmp_Options.inJustDecodeBounds设置为true将被持续用于对'BitmapFactory.decodeStream()'的后续调用吗? –

+0

是的,我说的是真的。由于某种原因,如果没有testSize方法,它将正确地运行相同的输入流和相同的源。我发现这是一个非常奇怪的行为,我不得不将它报告为一个错误。 –

+0

是的,我会说这肯定不会是预期的行为。我不确定是否有另一种方式从输入流中获取图像大小。 –

回答

9

的主要问题是不与BitmapFactory.Options,但与InputStream。数据流是连续的,因此当您读取数据流以仅对图像进行解码时,数据流中的指针正在移动。接下来,当您再次调用decode来实际解码位图时,流指针不再位于位图的开头,并且您得到null,因为部分流不能被解码。

您需要重置流。根据流的内容,您可能可以使用.mark.reset。从本质上讲,你会做这样的事情:

private Boolean testSize(InputStream inputStream) { 
    BitmapFactory.Options Bitmp_Options = new BitmapFactory.Options(); 
    Bitmp_Options.inJustDecodeBounds = true; 

    inputStream.mark(inputSteram.available()); 

    BitmapFactory.decodeResourceStream(getResources(), new TypedValue(), inputStream, new Rect(), Bitmp_Options); 

    inputSteram.reset(); 

    int currentImageHeight = Bitmp_Options.outHeight; 
    int currentImageWidth = Bitmp_Options.outWidth; 
    Bitmp_Options.inJustDecodeBounds = false; 

    if(currentImageHeight < 200 || currentImageWidth < 200){ 
     Object obj = map.remove(pageCounter); 
     Log.i("Page recycled", obj.toString()); 
     return true; 
    } 

    return false; 
} 

您可以检查是否您InputStream通过调用它markSupported支持此与否。如果返回true那么你可以使用上述技术。如果它返回false,那么上述方法将不起作用,并且在解码完全位图之前,您实际上需要再次关闭并重新打开该流。

+0

谢谢。你救了我好几个小时试图弄清楚这一点。再次感谢您告诉我这是连续的。 –

+0

啊,没有意识到你是在一个输入流的同一个实例上调用它。这就说得通了。 –

+0

不错的答案!不幸的是markSupported在我的情况下设置为false :( – Zhar