0

I need to start my intent after finishing the download of the image from the url without any action from the user of the application itself.

This is my activity which first will download the image and after that it will start the intent.

    //download image then start decod intent
public  void download(View v)
{
   //first download image 
    new MyAsnyc().execute();

      //then start this intent
      final Handler handler=new Handler();
     final Runnable r = new Runnable()
     {
         public void run() 
         {
             { 
                 Intent intent1 = new Intent(Test_PROJECTActivity.this, DecodeActivity.class);
                File path = Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                File file = new File(path, "DemoPictureX.png");
                Log.d("file", file.getAbsolutePath());
                intent1.putExtra("file", file.getAbsolutePath());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                startActivity(intent1);
                }
         }
     };
 handler.postDelayed(r, 5000);
  }

The MyAsnyc does fine and downloads the image correctly, but the second part from the code which starts the intent while downloading the image so the image will be corrupted so it causes an exception.

How can I make the intent start ONCE the image will be ready and finished download?

DisplayName
  • 3,093
  • 5
  • 35
  • 42
Start
  • 41
  • 5

3 Answers3

1

I think you should work with AsyncTask which has functions like onPostExecute, onPreExecute etc. Which you can easily control stuff before and after downloading.

Tuna Karakasoglu
  • 1,262
  • 9
  • 28
1
 public class YourClassName extends AsyncTask<String, Void, String > {

       protected void onPreExecute() { }

       protected String doInBackground(String... params) {}

       protected void onPostExecute(String result) {}
}

Do all your downloading stuff in the doInBackground method and start a new Intent from the onPostExecute method like this:

Intent i = new Intent(ClassName.this, TheClassToStart.class);
context.startActivity(i); 

if you want to put the image to the new activity simple do this:

i.putExtras(...)

Hope this help!

Regarding your Problem with the No enclosing instance

If your start the AsyncTask from another activity, pass this activity's context to the AsyncTask class.

public class YourClassName extends AsyncTask<String, Void, String > {
    Context mContext;

    public YourClassName(Context mContext) {
      this.mContext = mContext;
    }

    //other methods
}

From your first Activity, call the class that extends AsyncTask this way:

new YourClassName(getApplicationContext()).execute(""); 
Tobias Moe Thorstensen
  • 8,861
  • 16
  • 75
  • 143
  • i do this as you say "" Intent intent1 = new Intent(Test_PROJECTActivity.this, DecodeActivity.class); "" – Start Jul 25 '12 at 14:12
  • and i had this error "" No enclosing instance of the type Test_PROJECTActivity is accessible in scope "" – Start Jul 25 '12 at 14:12
0

You should put the Intent starting code inside AsyncTask's onPostExecute() method. This will make sure that the code is executed at the right time and it makes the exception handling a lot easier. Hope this helps.

Egor
  • 39,695
  • 10
  • 113
  • 130
  • but this make error at this statement Intent intent1 = new Intent(Test_PROJECTActivity.this, DecodeActivity.class); – Start Jul 25 '12 at 13:46
  • if i do what you say there is error at this part when write this at AsyncTask's onPostExecute() method – Start Jul 25 '12 at 13:47
  • with argument of intent constructor – Start Jul 25 '12 at 13:50
  • this error "" No enclosing instance of the type Test_PROJECTActivity is accessible in scope "" – Start Jul 25 '12 at 14:13
  • @Start, You should provide your Activity instance to AsyncTask to execute this method, plain and simple. – Egor Jul 25 '12 at 14:50
  • i do this to avoid the syntax error ---- Test_PROJECTActivity t=new Test_PROJECTActivity(); Intent intent1 = new Intent(t, DecodeActivity.class); ---- is this correct cause there is no starting for the intent after download the image – Start Jul 25 '12 at 14:59