位图工厂给出了空位图

时间:2012-05-13 17:46:13

标签: java android bitmap inputstream

我无法弄清楚为什么方法返回null而不是位图。我要做的是读取位图的尺寸并创建一个更小的新位图。我试图遵循这个Strange out of memory issue while loading an image to a Bitmap object

请帮助

private Bitmap LoadImageFromWebOperations(String url)
    {
          URL myFileUrl =null;          
          try {
               myFileUrl= new URL(url);
          } catch (MalformedURLException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
    try
    {
    /*InputStream is = (InputStream) new URL(url).getContent();
    Drawable d = Drawable.createFromStream(is, "src name");
    bitmap = ((BitmapDrawable)d).getBitmap().copy(Config.ARGB_8888, true);*/

         HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
         conn.setDoInput(true);
         conn.connect();
         InputStream is = conn.getInputStream();
         BitmapFactory.Options o = new BitmapFactory.Options();
         o.inJustDecodeBounds = true;
         BitmapFactory.decodeStream(is,null,o);

         int IMAGE_MAX_SIZE=960;


         int scale = 1;
         if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
             scale = (int)Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
         }
        o.inJustDecodeBounds = false;
         is.close();
         HttpURLConnection conn2= (HttpURLConnection)myFileUrl.openConnection();
         conn2.setDoInput(true);
         conn2.connect();
         InputStream is2 = conn2.getInputStream();
         BitmapFactory.Options o2 = new BitmapFactory.Options();
         o2.inMutable=true; 
         o2.inSampleSize = scale;
         o2.inPreferredConfig=Config.ARGB_8888;
         o2.inTempStorage = new byte[32*1024];

         o2.inJustDecodeBounds = true;
         bitmap = BitmapFactory.decodeStream(is2, null,o2);

         Log.d("nothing>>>>>>>>>>", String.valueOf(o2.outHeight));

   //  bitmap=bitmap1.copy(Bitmap.Config.ARGB_8888, true);
    //bitmap1.recycle();    
    return bitmap;
    }catch (Exception e) {
    System.out.println("Exc="+e);
    return null;
    }
    }``

1 个答案:

答案 0 :(得分:4)

您将inJustDecodeBounds设置为true,如果您将此字段设置为null,则解码器将始终返回true