0

我正在使用以下方法从 url 获取位图,但未显示图像。图片是一个大约 50x50px 的 png 文件。谢谢你。

public static Bitmap getBitmapFromURL(String src) {
         try {
             Log.e("src",src);
             URL url = new URL(src);
             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
             connection.setDoInput(true);
             connection.connect();
             InputStream input = connection.getInputStream();

             BitmapFactory.Options options = new BitmapFactory.Options();
             options.inJustDecodeBounds = true;


             BitmapFactory.decodeStream(input,null,options);
             Log.e("Bitmap","returned");

             //The new size we want to scale to
             final int REQUIRED_SIZE=70;

             //Find the correct scale value.
             int scale=1;
             while(options.outWidth/scale/2>=REQUIRED_SIZE && options.outHeight/scale/2>=REQUIRED_SIZE)
                 scale*=2;

             //Decode with inSampleSize
             BitmapFactory.Options o2 = new BitmapFactory.Options();
             o2.inSampleSize=scale;
             return BitmapFactory.decodeStream(input, null, o2);


         } catch (IOException e) {
             e.printStackTrace();
             Log.e("Exception",e.getMessage());
             return null;
         }


     }
4

1 回答 1

0

InputStream的被“吃掉了”。您只能使用一次流,因此当您读取位图标题以缩放图像时,您的输入流会被“吃掉”。您需要创建一个新的 InputStream 才能获得真正的位图。

于 2012-06-15T15:30:45.967 回答