异步任务停止工作,在使用断点进行调试时有效 - Android

时间:2014-09-17 05:14:24

标签: java android asynchronous android-asynctask android-camera

我的异步任务正在进行繁重的图像处理,单个异步任务成功运行但如果我尝试执行多个异步任务,即在第一个任务完成之前它停止工作...... 虽然它在我调试和插入时有完美的工作有断点来检查问题。

我只需要在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;
    }
}

为了避免过度处理我尝试连续运行而不是并行运行,手动执行此操作我做了这个计时器。

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);

在相机的活动结果上,创建了异步任务并将其插入到processList(AsyncTask的ArrayList)中

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();
            }

        }

这是我的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.

4 个答案:

答案 0 :(得分:1)

我认为在您的情况下,您应该使用Android服务或Intent服务。 链接:http://developer.android.com/guide/components/services.html

您不应该使用Async进行长时间的繁重操作。 在我的情况下,我使用了Intent Service和新的Runnables并且它有效。 如果您有更多问题,请询问。

答案 1 :(得分:1)

看看你的logcat,最后一行很有意思:

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

这个问题的重要性在我看来是因为它在调试期间有效 - 当断点给它足够的时间赶上来时!

作为一般规则,@ dadecki可能是正确的:对于Android上的这么多处理,你应该使用后台服务。但是,如果你不想追求这条道路,那么问这个问题似乎是明智的:为什么应用程序在主线程上做了太多工作。几点建议:

  • 尽可能多地从onPostExecute移动到doInBackground - 那里有很多代码!
  • 同时查看应用程序在主线程中正在执行的操作:代码中是否还有其他内容应该是异步任务(或服务)。

一旦你处理了这个问题,你就会处在一个更好的地方,以确定什么仍然是错误的(如果有的话)。

答案 2 :(得分:1)

对于您的情况,建议使用use AsyncTaskLoader。因为在某个时候你可以拥有有限数量的AsynTask。请this answer.在调试中,它可能正常工作,因为在断点处,系统有时间完成待处理的AsyncTask&amp;这就是调试时最大池大小永远不会达到的原因。

由于

答案 3 :(得分:0)

增加线程睡眠时间,现在看它是1秒,使其达到5秒或10秒..

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