Rob*_*oss 87 java android image bitmap
我有问题BitmapFactory.decodeStream(inputStream).在没有选项的情况下使用它时,它将返回一个图像.但是当我将它与选项一起使用时,.decodeStream(inputStream, null, options)它永远不会返回Bitmaps.
我要做的是在实际加载之前对Bitmap进行下采样以节省内存.我读过一些很好的指南,但都没有使用.decodeStream.
工作很精细
URL url = new URL(sUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is, null, options);
Run Code Online (Sandbox Code Playgroud)
什么都不行
InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is, null, options);
InputStream is = connection.getInputStream();
Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, options);
Boolean scaleByHeight = Math.abs(options.outHeight - TARGET_HEIGHT) >= Math.abs(options.outWidth - TARGET_WIDTH);
if (options.outHeight * options.outWidth * 2 >= 200*100*2){
// Load, scaling to smallest power of 2 that'll get it <= desired dimensions
double sampleSize = scaleByHeight
? options.outHeight / TARGET_HEIGHT
: options.outWidth / TARGET_WIDTH;
options.inSampleSize =
(int)Math.pow(2d, Math.floor(
Math.log(sampleSize)/Math.log(2d)));
}
// Do the actual decoding
options.inJustDecodeBounds = false;
Bitmap img = BitmapFactory.decodeStream(is, null, options);
Run Code Online (Sandbox Code Playgroud)
Rob*_*oss 111
问题是,一旦您使用来自HttpUrlConnection的InputStream来获取图像元数据,您就无法再次回放并再次使用相同的InputStream.
因此,您必须为图像的实际采样创建新的InputStream.
Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, options);
Boolean scaleByHeight = Math.abs(options.outHeight - TARGET_HEIGHT) >= Math.abs(options.outWidth - TARGET_WIDTH);
if(options.outHeight * options.outWidth * 2 >= 200*200*2){
// Load, scaling to smallest power of 2 that'll get it <= desired dimensions
double sampleSize = scaleByHeight
? options.outHeight / TARGET_HEIGHT
: options.outWidth / TARGET_WIDTH;
options.inSampleSize =
(int)Math.pow(2d, Math.floor(
Math.log(sampleSize)/Math.log(2d)));
}
// Do the actual decoding
options.inJustDecodeBounds = false;
is.close();
is = getHTTPConnectionInputStream(sUrl);
Bitmap img = BitmapFactory.decodeStream(is, null, options);
is.close();
Run Code Online (Sandbox Code Playgroud)
Jet*_*ieh 29
尝试使用BufferedInputStream包装InputStream.
InputStream is = new BufferedInputStream(conn.getInputStream());
is.mark(is.available());
// Do the bound decoding
// inJustDecodeBounds =true
is.reset();
// Do the actual decoding
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
64888 次 |
| 最近记录: |