`
wang_peng1
  • 浏览: 3903638 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

BitmapFactory.decodeStream使用问题

 
阅读更多

HttpURLConnection connection  = (HttpURLConnection) url.openConnection();

InputStream is = connection.getInputStream();使用HttpURLConnection产生的流只能使用一次
如果你想连续调用这个流 必须重新初始化 

例如
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){
        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();
分享到:
评论
3 楼 fairy1314 2013-02-21  
使用HttpURLConnection产生的流只能使用一次,如果你想连续调用这个流 必须重新初始化.

应该是所有的InputStream,
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, options);
这里已经用过这个is了,第二次需要重新初始化。
2 楼 snowdream 2012-12-10  
那请问这种情况下怎么重用?
1 楼 xinyao 2012-03-07  
is
很靠谱啊,呵呵

相关推荐

    android内存优化之图片优化

    因此,改用先通过BitmapFactory.decodeStream方法,创建出一个bitmap,再将其设为ImageView的source,decodeStream最大的秘密在于其直接调用JNI>>nativeDecodeAsset()来完成decode,无需再使用java层的createBitmap...

    Android Bitmap的加载与缓存

    Android系统中图片一般用Bitmap对象表示,它支持png,jpg等常见格式。通常情况下图片的体积都比较大,单个应用允许使用的内存又是... BitmapFactory.decodeStream :从输入流加载。 BitmapFactory.decodeResource :从

    Android HttpURLConnection 读取网络图片.rar

    b=(Button)this.findViewById(R.id.b);//图片浏览按钮  et=(EditText)this.findViewById(R.id.et);... bitmap = BitmapFactory.decodeStream(is);//将InputStream变成Bitmap  is.close();//关闭InputStream

    android 调用相机显示拍摄后的图片

    Bitmap map=BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options); android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), map, null, null); ...

    Bitmap生成时序图

    这是博文《BitmapFactory.decodeResource VS BitmapFactory.decodeStream》中的时序图,发现上传后,看不清楚,所以上传在这里。

    Android 设置手机屏幕壁纸(桌面背景).rar

     bm = BitmapFactory.decodeStream(is);  /* 关闭InputStream */  is.close();  /* 让ImageView显示图像 */  imtp.setImageBitmap(bm);//显示下载图片  Sample8_12_Activity.this.setWallpaper(bm);//将...

    ViewPager或ImgeView加载图片出现内存溢出(OOM)

    使用setImageResource或BitmapFactory.decodeResource来设置一张大图,因为这些函数在完成decode后,最终都是通过java层的createBitmap来完成的,需要消耗更多内存。解决方案:改用先通过BitmapFactory.decodeStream...

    图片缓存机制代码

    用于类似图库,缓存,所困、... bitmap = BitmapFactory.decodeStream(new FileInputStream(imagePath), null, options); } catch (FileNotFoundException e) { e.printStackTrace(); } return bitmap; } }

    android 如何从网络获取一张图片并显示

    如何从网络中获取一张图片,并显示出来?? 首先应想到若要从网络资源中获取图片,就需要通过流操作,于是就想到如何创建流...第四步:利用BitmapFactory.decodeStream()方法直接将图片流解码为bitmap 第五步:送去显示

    android Bitmap用法总结

    Bitmap bm = BitmapFactory.decodeStream(getResources() .openRawResource(R.drawable.dog)); // 获得图片的宽高 int width = bm.getWidth(); int height = bm.getHeight(); // 设置想要的大小 int newWidth = 320;...

    Android 中对于图片的内存优化方法

    因此,改用先通过 BitmapFactory.decodeStream 方法,创建出一个 bitmap,再将其设为 ImageView 的 source,decodeStream 最大的秘密在于其直接调用 JNI>>nativeDecodeAsset() 来完成 decode,无需再使用 Java

    ImageView简单加载网络图片实例代码

    代码如下: private void loadImage() { mImageView = (ImageView) findViewById(R.id.... mImageView.setImageBitmap(BitmapFactory.decodeStream(url.openStream())); } catch (Exception e) { } } 您可能感兴趣的文

    Android实现图片自动轮换

    Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri)); ImageView imageView = (ImageView) findViewById(R.id.imgview_src); /* 将Bitmap设定到ImageView */ imageView.setImageBitmap(bitmap...

    Android Bitmap的加载优化与Cache相关介绍

    BitMapFactory 提供了四类方法: decodeFile,decodeResource,decodeStream 和 decodeByteArray 分别用于从文件系统,资源,输入流以及字节数组中加载出一个 Bitmap 对象。 高效加载 Bitmap 很简单,即采用 BitMapFactory...

    炫舞吧 android 游戏开发

    import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.media.AudioManager; import android.media.MediaPlayer; import android.media....

Global site tag (gtag.js) - Google Analytics