1

I am trying to download images from URL. The image type is PNG and the resolution is 400x400 pixels.

Here is the download code snippet.

Bitmap bitmap=null;
URL imageUrl = new URL(url);
conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream ins=conn.getInputStream();
os = new FileOutputStream(f);
Utilities.getUtilities().copyStream(ins, os);
os.flush();

Log.i(TAG_NAME, "file size : "+ f.length());
Log.i(TAG_NAME, "file exists in cache? " + f.exists());
bitmap = decodeFile(f);
return bitmap;

Here is the file writer.

public void copyStream(InputStream is, OutputStream os) {
  final int buffer_size=1024;
    try
    {
        byte[] bytes=new byte[buffer_size];
        for(;;)
        {
          int count=is.read(bytes, 0, buffer_size);
          if(count==-1)
              break;
          os.write(bytes, 0, count);
        }
    }
    catch(Exception ex){
        ex.printStackTrace();
    }
}

And the decode method

private Bitmap decodeFile(File f){
   //decode image size
   BitmapFactory.Options o = new BitmapFactory.Options();
   o.inJustDecodeBounds = true;
   try {
     BitmapFactory.decodeStream(new FileInputStream(f));
   } catch (FileNotFoundException e) {
      e.printStackTrace();
   }

   final int REQUIRED_SIZE = 400; //for testing, it is set to b a constant
   System.out.println("REQUIRED_SIZE >>> " + REQUIRED_SIZE);
   int width_tmp=o.outWidth, height_tmp=o.outHeight;
   int scale=1;
   while(true){
      if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
            break;
      width_tmp/=2;
      height_tmp/=2;
        scale*=2;
    }

    //decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    //o2.inJustDecodeBounds = true;
    o2.inPreferredConfig = Bitmap.Config.ARGB_8888;
    o2.inSampleSize=scale; //scale is set off since android:src automatically scales the image to fit the screen

    try {
        return BitmapFactory.decodeStream(new FileInputStream(f));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

}

I can see that the file exists in the device. However, the decode stream is failing. I spent hours searching on the internet; tried almost everything, no success and almost my heads rolling.

Decode streams is causing the following error.

SkImageDecoder::Factory returned null

Do you find anything missing here?

EDIT:

Issue is now solved. The server was expecting cookie details which I failed to attach. Spent almost a day, beating around the bushes :-)

Thanks all for the valuable comments!

mmlooloo
  • 18,937
  • 5
  • 45
  • 64
Renjith
  • 3,457
  • 5
  • 46
  • 67

1 Answers1

0

IMO, you may want to re-evaluate merits of httpurlconn vs native httpclient implementation. Android/google go for httpurlconn but, many opt to take greater control of low level details surrounding net protocol.

Here is sample async httpclient that wraps in bitmap handler. You can easily extend the sample method=processBitmapEntity() with your rules affecting bmp size.

Sample getbitmap url:

  public  int getBitmap(String mediaurl, int ctr){

           Handler handler = new Handler() {
               public void handleMessage(Message message) {
                 switch (message.what) {
                 case HttpConnection.DID_START: {
                   Log.d(TAG, "Starting connection...");
                   break;
                 }
                 case HttpConnection.DID_SUCCEED: {
                     //message obj is type bitmap
                     Log.d(TAG, "OK bmpARRAY " +message.arg1); 
                    Bitmap response = (Bitmap) message.obj;
                   break;
                 }
                 case HttpConnection.DID_ERROR: {

                   Exception e = (Exception) message.obj;
                   e.printStackTrace();
                   Log.d(TAG, "Connection failed.");
                   break;
                 }
               }
             }
           };        
           new HttpConnection(handler, PreferenceManager.getDefaultSharedPreferences(this), ctr).bitmap(mediaurl);

       return -1;  

And the bitmap handler in HttpConnection class that is part of the link sample above:

private void processBitmapEntity(HttpEntity entity) throws IOException {
    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
    Bitmap bm = BitmapFactory.decodeStream(bufHttpEntity.getContent());
    handler.sendMessage(Message.obtain(handler, DID_SUCCEED, bm));
}

And a git project

artex
  • 1,776
  • 2
  • 11
  • 16
Robert Rowntree
  • 6,230
  • 2
  • 24
  • 43