Don*_*ing 4 android inputstream bitmap bitmapfactory
我需要获得输入流中找到的图像的高度和宽度.这是我做的:
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;}
Run Code Online (Sandbox Code Playgroud)
跳到问题点:
即使我在下面的第二个方法计算后强制它为false,它也会更改BitmapFactory.Options.
private Bitmap getBitmap(InputStream InpStream){
Bitmap originalBitmap = BitmapFactory.decodeStream(InpStream);//Null.
return originalBitmap;
}
Run Code Online (Sandbox Code Playgroud)
现在我的问题是有另一种方法从输入流获取图像的大小和宽度?我真的需要帮助,非常感谢任何帮助.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
主要问题不在于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;
}
Run Code Online (Sandbox Code Playgroud)
您可以InputStream通过调用来检查您是否支持此功能markSupported.如果返回true则可以使用上述技术.如果它返回false,则上述方法将不起作用,您实际上需要在解码完整位图之前再次关闭并重新打开流.
| 归档时间: |
|
| 查看次数: |
3999 次 |
| 最近记录: |