android远程位图未显示

时间:2012-06-15 15:27:34

标签: java android eclipse

我使用以下方法从url获取位图,但不显示图像。 Image是一个大约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;
         }


     }

1 个答案:

答案 0 :(得分:0)

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