-1

i can't figure out why method is returning me null instead of a bitmap. What i'm trying to do is to read the dimension of the bitmap and create a new one that is smaller. I was trying to follow this Strange out of memory issue while loading an image to a Bitmap object

help please

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;
    }
    }``
Community
  • 1
  • 1
youssoua
  • 802
  • 1
  • 11
  • 20

1 Answers1

3

you are setting inJustDecodeBounds to true and the decoder will always return null if you set this field to true

Blackbelt
  • 156,034
  • 29
  • 297
  • 305