我正在尝试采用bitmap resizing教程 – 唯一的区别是我使用decodeStream而不是decodeResource.这很奇怪,但是没有任何 *** 作的decodeStream给了我一个位图obj,但是当我通过decodeSampledBitmapFromStream时它会因某种原因返回null.
我如何解决它 ?
这是我使用的代码:
protected Handler _onPromoBlocksLoad = new Handler() { @OverrIDe public voID dispatchMessage(Message msg) { PromoBlocksContainer c = (PromoBlocksContainer) _promoBlocksFactory.getResponse(); httpRequest request = new httpRequest(c.getPromoBlocks().get(0).getSmallthumbnail()); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; inputStream stream; ImageVIEw v = (ImageVIEw) findVIEwByID(R.ID.banner); try { stream = request.getStream(); //v.setimageBitmap(BitmapFactory.decodeStream(stream)); Works fine Bitmap img = decodeSampledBitmapFromStream(stream, v.getWIDth(), v.getHeight()); v.setimageBitmap(img); } catch (IOException e1) { // Todo auto-generated catch block e1.printstacktrace(); } } }; public static int calculateInSampleSize(BitmapFactory.Options options, int reqWIDth, int reqHeight) { final int height = options.outHeight; final int wIDth = options.outWIDth; int inSampleSize = 1; if (height > reqHeight || wIDth > reqWIDth) { if (wIDth > height) { inSampleSize = Math.round((float)height / (float)reqHeight); } else { inSampleSize = Math.round((float)wIDth / (float)reqWIDth); } } return inSampleSize; } public static Bitmap decodeSampledBitmapFromStream(inputStream res, int reqWIDth, int reqHeight) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(res, null, options); options.inSampleSize = calculateInSampleSize(options, reqWIDth, reqHeight); options.inJustDecodeBounds = false; Bitmap img = BitmapFactory.decodeStream(res, null, options); // Gives null return img; }
解决方法:
问题是,一旦你使用了来自httpUrlConnection的inputStream,你就不能再回放并再次使用相同的inputStream.因此,您必须为图像的实际采样创建新的inputStream.否则我们必须中止http请求.
request.abort();
总结 以上是内存溢出为你收集整理的android – decodeStream返回null全部内容,希望文章能够帮你解决android – decodeStream返回null所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)