-1

我不知道为什么方法返回 null 而不是位图。我要做的是读取位图的尺寸并创建一个更小的新尺寸。在将图像加载到位图对象时,我试图关注这个奇怪的内存不足问题

请帮忙

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;
    }
    }``
4

1 回答 1

3

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

于 2012-05-13T17:50:49.373 回答