0

guys. I have this code:

package com.example.httpprogress;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;

public class MyPicGetTask extends AsyncTask<URL , Void, Bitmap>{


    InputStream is = null;
    BufferedInputStream bis = null;
    Bitmap bmp = null;


    @Override
    protected Bitmap doInBackground(URL... urls) {
        // TODO Auto-generated method stub

        URL url = urls[0];


        try {
               URLConnection conn = url .openConnection();
               conn.connect();
               is = conn.getInputStream();
               bis = new BufferedInputStream( is );
               bmp = BitmapFactory.decodeStream( bis );
            } catch (MalformedURLException e) {

            } catch (IOException e) {

            } finally {
               try {
                  is.close();
                  bis.close();
               } catch (IOException e) {

               }
            }
        return bmp;


    }

}

it fails, but if i use AsyncTask and describe this class as inner in my activity - it's ok . I can not say the reason because i can not debug, i can see that debug tab opens when it fails but it is not informative for me. Any ideas? Sorry for my noob question

that's my Activity:

package com.example.httpprogress;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class PicActivity extends Activity implements OnClickListener{

    InputStream is = null;
    BufferedInputStream bis = null;
    Bitmap bmp = null;
    private URL url;
    //"http://192.168.0.30/03.jpg";
     /*
    private class getPicTask extends AsyncTask<Void, Void, Void>{

        @Override
        protected Void doInBackground(Void... s) {
            // TODO Auto-generated method stub


            try {
                url = new URL("http://192.168.0.93/image.php");
            } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            try {
                   URLConnection conn = url .openConnection();
                   conn.connect();
                   is = conn.getInputStream();
                   bis = new BufferedInputStream( is );
                   bmp = BitmapFactory.decodeStream( bis );
                } catch (MalformedURLException e) {

                } catch (IOException e) {

                } finally {
                   try {
                      is.close();
                      bis.close();
                   } catch (IOException e) {

                   }
                }
            return null;


        }



    };

    */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pic);




        final ImageView image = (ImageView) findViewById(R.id.imageView1);


        image.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {


        ///////////

                try {
                    url =  new URL("http://192.168.0.30/03.jpg");
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                new MyPicGetTask().execute(url);


                image.setImageBitmap(bmp);


            }
        });


            ////////////////
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.pic, menu);


        ////////////////


        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Log.d("httpProgress", "Onclick()");




    }

}

4 Answers4

1

Add Log.d() code to doInBackground(...) to print out all exceptions that occur. That should tell you what's going wrong, e.g.

try {
    URLConnection conn = url .openConnection();
    conn.connect();
    is = conn.getInputStream();
    bis = new BufferedInputStream( is );
    bmp = BitmapFactory.decodeStream( bis );
} catch (Exception e) {
    Log.d("Async","EXCEPTION",e);
} finally {
    try {
        is.close();
        bis.close();
    } catch (IOException e) {
        Log.d("Close","EXCEPTION",e);
    }
}
Stochastically
  • 7,616
  • 5
  • 30
  • 58
  • thanks, i have forgotten that i have shut down my vm with web server, (i know it, becase i use your code), i will use it in future – Antony Tren'kin Apr 11 '13 at 19:53
0

The bitmap you return from doInBackground should then be used to update your UI in onPostExecute.

protected void onPostExecute(Bitmap bitmap) {
    image.setImageBitmap(bitmap);
}

Your asyncTask subclass needs access to image in order to update the UI, so having it as a inner class is one way to make sure it can do this.

  • thanks, it's useful but not necessary, i can use get function. right? And it can't be done if i create MyTask as non-inner – Antony Tren'kin Apr 11 '13 at 19:56
  • If you keep MyPicGetTask as a standalone class, you could make it work by using a callback interface to let you know when the async task is complete. http://stackoverflow.com/questions/3398363/how-to-define-callbacks-in-android/3398416#3398416 – Christopher Moore Apr 11 '13 at 20:01
  • ou, thatnks . But it looks too deep for my level of Java and Android but anyway i will try it later – Antony Tren'kin Apr 11 '13 at 20:06
0

When the MyPicGetTask is an inner class it has access to the bmp field. When you pulled it out of your activity it lost access to the bmp class field.

I would suggest reading Google's documentation and following their examples for AsyncTasks.

Andrew Halloran
  • 1,518
  • 1
  • 14
  • 16
0

Your AsyncTask if you're using it as a public class outside the activity in which you are calling it needs to recieve the context of that activity. There are a number of posts here, here and here that explain how to set this up.

Community
  • 1
  • 1
Rarw
  • 7,645
  • 3
  • 28
  • 46