android decodestream,Android开发中,BitmapFactory.decodeStream(InputStream is, Rect outPadding, Options ...

满意答案

02ae427d08e371d7e90d5b995e828d6d.png

soucuqi382

2014.05.19

02ae427d08e371d7e90d5b995e828d6d.png

采纳率:41%    等级:10

已帮助:970人

public static Bitmap decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts)

Since: API Level 1

Decode an input stream into a bitmap. If the input stream is null, or

cannot be used to decode a bitmap, the function returns null.

The stream's position will be where ever it was after the encoded data

was read.

Parameters

is

The input stream that holds the raw data to be decoded into a bitmap.

outPadding

If not null, return the padding rect for the bitmap if it exists, otherwise set padding to [-1,-1,-1,-1]. If

no bitmap is returned (null) then padding is

unchanged.

opts

null-ok; Options that control downsampling and whether the image should be completely decoded, or just is size returned.

Returns

The decoded bitmap, or null if the image data could not be

decoded, or, if opts is non-null, if opts requested only the

size be returned (in opts.outWidth and opts.outHeight)

习惯从API了解

03分享举报

当调用`BitmapFactory.decodeStream(inputStream)`方法返回`null`时,可能是以下原因之一: 1. `inputStream`为空或已关闭,因此需要确保输入流是正确打开并可用的。 2. 图片格式不受支持,例如图片格式为WebP或HEIF,可以使用`Options.inPreferredConfig`设置合适的图片格式。 3. 图片太大,超出了内存限制,可以通过设置`Options.inSampleSize`来减小图片大小,或者使用`BitmapFactory.decodeFile()`方法,将图片保存在本地并逐步加载。 4. 图片压缩方式不受支持,例如图片采用了透明压缩方式,可以使用`Options.inPreferredConfig`设置合适的图片压缩方式。 解决方法: 1. 确保输入流可用并未关闭。 2. 设置合适的图片格式和压缩方式。 3. 减小图片大小或者采用其他方式加载图片。 4. 检查代码是否正确,可以参考以下代码: ``` Bitmap bitmap = null; try { InputStream inputStream = getContentResolver().openInputStream(uri); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(inputStream, null, options); inputStream.close(); int imageWidth = options.outWidth; int imageHeight = options.outHeight; int scaleFactor = Math.min(imageWidth / targetWidth, imageHeight / targetHeight); inputStream = getContentResolver().openInputStream(uri); options.inJustDecodeBounds = false; options.inSampleSize = scaleFactor; options.inPurgeable = true; options.inInputShareable = true; options.inPreferredConfig = Bitmap.Config.RGB_565; bitmap = BitmapFactory.decodeStream(inputStream, null, options); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } ``` 注意:这里的`uri`是图片的URI,`targetWidth`和`targetHeight`是目标宽度和高度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值