java - 异步任务停止工作,在使用断点调试时工作 - Android

标签 java android asynchronous android-asynctask android-camera

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

我只需要在ActivityResult上有断点

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

根据相机的 Activity 结果,创建异步任务并将其插入到 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();
            }

        }

这是我的日志

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.

最佳答案

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

您不应该将 Async 用于长时间的繁重操作。 在我的例子中,我使用了带有新 Runnables 的 Intent Service 并且它起作用了。 如果您有更多问题,请提出。

关于java - 异步任务停止工作,在使用断点调试时工作 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25882768/

相关文章:

java - 如何使用 CompletionHandlers 和小于请求的 ByteBuffer 读取请求?

java - 清除/取消设置系统属性 maven.compiler.release 以便它不再存在

java - mysql/jdbc : Deadlock

android - 无法记录某些行

javascript - 使用 async.js 系列识别回调数据

javascript - 异步请求回调,传递的变量在我不希望它们被覆盖时被覆盖

java - Android:IntelliJ IDEA 10 - Android GUI 设计器在哪里?

java - 从 websocket 获取 HttpSession 的 Jetty/javalin 总是返回 null

android - 标记布局未在 map 中正确显示

android - 缺少样式。是否为此布局选择了正确的主题?