1

My Async task is doing heavy image processing, a single Async task works successfully but if i try to execute multiple Async tasks i.e. before the first one is completed it stops working... Though it works perfectly when i debug and insert have break points to check the problem.

I only need to have break point onActivityResult

    private class ProcessVideoTask extends AsyncTask<String, Void, Void> {

    int goodPointsLocal;
    int badPointsLocal;
    String videoFileLocal;
    ArrayList<Integer> xposLocal;
    ArrayList<Integer> yposLocal;
    ArrayList<String> markerSelectedLocal;
    int windowXxLocal;
    int windowYyLocal;

    public ProcessVideoTask(int _goodPoints, int _badPoints,
            String _videoFile, ArrayList<Integer> _xpos,
            ArrayList<Integer> _ypos, ArrayList<String> _markerSelected,
            int _windowXx, int _windowYy) {
        // TODO Auto-generated constructor stub

        goodPointsLocal = _goodPoints;
        badPointsLocal = _badPoints;
        videoFileLocal = _videoFile;
        xposLocal = _xpos;
        yposLocal = _ypos;
        markerSelectedLocal = _markerSelected;
        windowXxLocal = _windowXx;
        windowYyLocal = _windowYy;

    }

    // automatically done on worker thread (separate from UI thread)
    protected Void doInBackground(final String... args) {
        notGotoBack = false;
        String path = videoFileLocal;
        ProcessVDOToImages(path);
        try {//get frames from video and edit individually (heavy processing)
            PutMarkersOnImages(xposLocal, yposLocal, markerSelectedLocal,
                    Constants.RT_EVALUATE_TRAINING, windowXxLocal,
                    windowYyLocal);
        } catch (Exception e) {

        }

        mFilePathMarkers = ProcessImagestoVdo();//append audio to frames (heavy)
        SetAudioWithMarkerVDO(mFilePathMarkers, videoFileLocal);

        return null;
    }

    protected void onPostExecute(final Void unused) {
        File savePathWMarker = new File(Environment
                .getExternalStorageDirectory().getPath()
                + "/HumanFocus/MarkerFrame/");

        File savePathWOMarker = new File(Environment
                .getExternalStorageDirectory().getPath()
                + "/HumanFocus/WithoutMarkerFrame/");

        if (savePathWMarker.isDirectory()) {
            String[] children = savePathWMarker.list();
            for (int i = 0; i < children.length; i++) {
                new File(savePathWMarker, children[i]).delete();
            }
        }

        if (savePathWOMarker.isDirectory()) {
            String[] children = savePathWOMarker.list();
            for (int i = 0; i < children.length; i++) {
                new File(savePathWOMarker, children[i]).delete();
            }
        }
        // change this if no Marker Video
        File src = new File("");
        File dst = new File("");
        if (mFilePathMarkers != null) {
            src = new File(mFilePathMarkers);
        }
        if (videoFileLocal != null) {
            dst = new File(videoFileLocal);
        }
        try {
            copyFile(src, dst);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        int maxNumber = 0;
        for (int i = 0; i < mCreateTraining.getAttachedFiles().size(); i++) {

            String fileName = mCreateTraining.getAttachedFiles().get(i)
                    .getDisplayName().substring(0, 2);

            if (isNumeric(fileName)) {
                if (Integer.parseInt(fileName) > maxNumber)
                    maxNumber = Integer.parseInt(fileName);
            }
        }

        mCreateTraining.attachFile(saveVideo(videoFileLocal,
                Constants.RT_CREATE_TRAINING, goodPointsLocal,
                badPointsLocal, maxNumber, videoFileLocal));
        updateReportItemsList();
        Toast.makeText(CreateTrainingActivity.this,
                "Video Processing is Successfully Completed",
                Toast.LENGTH_LONG).show();

        notGotoBack = true;
    }
}

To avoid over-processing i try to run it serially rather then parallel, to do it manually i made this timer.

Timer mTimer = new Timer();
    mTimer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            if (processList.size() > 0) {
                if (processList.get(0).getStatus() == AsyncTask.Status.FINISHED) {

                    processList.remove(0);

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    if (processList.size() > 0) {
                        processList.get(0).execute();
                    }
                }
            }
        }
    }, 0, 5000);

And on activity result from the camera, Async task is created and inserted in the processList (ArrayList of AsyncTask)

if (requestCode == REQUEST_CODE_RECORD_VIDEO_ACTIVITY) {
        if (data != null && data.getExtras() != null) {
            Toast.makeText(CreateTrainingActivity.this,
                    "Video is Processing.. Please Wait ...",
                    Toast.LENGTH_LONG).show();


            goodPoints = data.getExtras().getInt("goodPoints", 0);
            badPoints = data.getExtras().getInt("badPoints", 0);
            videoFile = data.getExtras().getString("data");
            Xpos = data.getIntegerArrayListExtra("Xpos");
            Ypos = data.getIntegerArrayListExtra("Ypos");
            MarkerSelected = data.getStringArrayListExtra("MarkerSelected");

            windowXx = data.getExtras().getInt("windowXx");
            windowYy = data.getExtras().getInt("windowYy");

            ///////////////////////////////////////////////// Put tasks into queue

            ProcessVideoTask mTask = new ProcessVideoTask(goodPoints,
                    badPoints, videoFile, Xpos, Ypos, MarkerSelected,
                    windowXx, windowYy);

            processList.add(mTask);

            if (processList.size() == 1) {
                processList.get(0).execute();
            }

        }

This is my logcat

09-17 10:09:23.108: E/sizeee addxxxxxxxxxx(21007): 96
09-17 10:09:23.148: I/dalvikvm-heap(21007): Grow heap (frag case) to 144.206MB for 11059216-byte allocation
09-17 10:09:23.218: I/dalvikvm-heap(21007): Grow heap (frag case) to 151.585MB for 7738604-byte allocation
09-17 10:09:23.248: I/millisUntilFinished:(21007): 1794
09-17 10:09:23.248: E/sizeee addxxxxxxxxxx(21007): 99
09-17 10:09:23.278: I/dalvikvm-heap(21007): Grow heap (frag case) to 144.206MB for 11059216-byte allocation
09-17 10:09:23.368: E/sizeee addxxxxxxxxxx(21007): 102
09-17 10:09:23.418: I/dalvikvm-heap(21007): Grow heap (frag case) to 144.206MB for 11059216-byte allocation
09-17 10:09:23.478: I/dalvikvm-heap(21007): Grow heap (frag case) to 151.585MB for 7738604-byte allocation
09-17 10:09:23.508: E/sizeee addxxxxxxxxxx(21007): 105
09-17 10:09:23.538: I/dalvikvm-heap(21007): Grow heap (frag case) to 144.206MB for 11059216-byte allocation
09-17 10:09:23.608: E/sizeee addxxxxxxxxxx(21007): 108
09-17 10:09:23.668: I/dalvikvm-heap(21007): Grow heap (frag case) to 144.206MB for 11059216-byte allocation
09-17 10:09:23.728: I/dalvikvm-heap(21007): Grow heap (frag case) to 151.585MB for 7738604-byte allocation
09-17 10:09:23.758: E/sizeee addxxxxxxxxxx(21007): 111
09-17 10:09:23.788: I/dalvikvm-heap(21007): Grow heap (frag case) to 144.207MB for 11059216-byte allocation
09-17 10:09:23.838: E/sizeee addxxxxxxxxxx(21007): 114
09-17 10:09:23.868: I/dalvikvm-heap(21007): Grow heap (frag case) to 151.586MB for 11059216-byte allocation
09-17 10:09:23.908: I/dalvikvm-heap(21007): Grow heap (frag case) to 158.966MB for 7738604-byte allocation
09-17 10:09:25.478: D/MediaRecorder(21007): Current Package Name: uk.org.humanfocus.hfi
09-17 10:09:25.488: I/Choreographer(21007): Skipped 98 frames!  The application may be doing too much work on its main thread.
09-17 10:09:25.488: I/millisUntilFinished:(21007): 1791
09-17 10:09:26.498: I/millisUntilFinished:(21007): 1790
09-17 10:09:27.508: I/millisUntilFinished:(21007): 1789
09-17 10:09:28.508: I/millisUntilFinished:(21007): 1788
09-17 10:09:29.508: I/millisUntilFinished:(21007): 1787
09-17 10:09:30.508: I/millisUntilFinished:(21007): 1786
09-17 10:09:30.678: I/user interaction(21007): called
09-17 10:09:30.678: I/millisUntilFinished:(21007): 1799
09-17 10:09:31.698: I/millisUntilFinished:(21007): 1798
09-17 10:09:31.698: I/On Pause :(21007): called
09-17 10:09:32.318: I/Choreographer(21007): Skipped 30 frames!  The application may be doing too much work on its main thread.
Salmaan
  • 3,543
  • 8
  • 33
  • 59
  • Use Listeners to know the execution status of the Async task check this link https://stackoverflow.com/questions/23534418/how-to-make-asynctask-modular-in-android/23534743#23534743 – Sainath Patwary karnate Sep 17 '14 at 05:19
  • am sorry but this log doesnt speak about any crash, can you take new logact dump with crash log and put it here. – Techfist Sep 17 '14 at 05:19

4 Answers4

1

I think that in your case you should use Android Service or Intent Service. Link:http://developer.android.com/guide/components/services.html

You shouldn't use Async for long heavy operations. In my case I used Intent Service with new Runnables in it and it worked. If you have more questions, please ask.

1

Looking at your logcat, the final line is interesting:

09-17 10:09:32.318: I/Choreographer(21007): Skipped 30 frames!  The application may be doing too much work on its main thread.

The importance of this is suggested to me by the fact that it works during debugging - when breakpoints give it plenty of time to catch up!

As a general rule, @dadecki is probably right: for this much processing on android you should probably use a background service. However, if you don't wish to pursue that path, it would seem seem sensible to ask the question: why is the app doing too much work on the main thread. A couple of suggestions:

  • Move as much as you can out of onPostExecute and into doInBackground - there sweems to be a lot of code there!
  • Look at what else the app is doing in the main thread at the same time: are there other things in your code that should really be in async tasks (or a service).

Once you've dealt with this issue, you will be in a much better place to determine what is still goin wrong (if anything).

Neil Townsend
  • 6,024
  • 5
  • 35
  • 52
1

For your case, it is recommended to use use AsyncTaskLoader. Because at a time you can have limited number of AsynTask. Please this answer. In debugging it may be working fine, because at break point, system gets a time to finish the pending AsyncTask & that's why maximum pool size will never reach in debugging.

Thanks

Community
  • 1
  • 1
hemu
  • 3,199
  • 3
  • 44
  • 66
0

Increase Thread sleep time and see now it is 1 sec make it to 5 sec or 10 sec..

 try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }