陷入了AsyncTask

时间:2014-07-09 15:27:39

标签: java android android-asynctask pdfviewer

我在AsyncTask中遇到了一些奇怪的行为。我正在尝试异步加载pdf到PDFView(I used this library)

这是我的代码:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_simple_fileview);

        setupUI();

        startFile();

    }

    private void startFile() {
        if(FileUtilities.getFileExtension(file.getName()).equals("pdf")) {

            new loadPDFTask(this).execute(file);

        }else{
            try{
                VideoView mVideoView = (VideoView)findViewById(R.id.videoview);
                mVideoView.setVisibility(View.VISIBLE);
                Uri uri = Uri.parse(file.getAbsolutePath());
                mVideoView.setVideoURI(uri);
                mVideoView.requestFocus();
                mVideoView.start();
            }catch (Exception e){
                Log.e("Fehler", "Video konnte nicht geladen werden");
            }
        }
    }

    private void setupUI() {
        String path = getIntent().getExtras().getString("file");
        file = new File(path);
        mNextFile = (ImageView)findViewById(R.id.next_file);
        mPreviousFile = (ImageView)findViewById(R.id.previous_file);
        pdfView = (PDFView) findViewById(R.id.pdfview);
    }


    //lädt PDF asynchron und zeigt ProgressDialog an
    private class loadPDFTask extends AsyncTask<Object,String,File> {

        private Context context;


        public loadPDFTask(Context ctx){
            context = ctx;
            dialog = new ProgressDialog(context);
            dialog.setMessage("Loading PDF...");
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog.show();
        }
        @Override
        protected File doInBackground(Object... args) {
            File file = (File)args[0];
            pdfView.fromFile(file)
                    .defaultPage(1)
                    .showMinimap(false)
                    .enableSwipe(true)
                    .load();
            return file;
        }
        @Override
        protected void onPostExecute(File unused) {
            super.onPostExecute(unused);
            if(dialog!=null) {
                dialog.dismiss();
            }
        }
    }

我的问题是,它执行onPreExecute方法并正确显示ProgressDialog。但是从那里它就会卡住并且正在装载和装载。

我检查了正在运行的Threads,看到AsyncTask线程都在等待。 我猜这与事实有关,我需要用ThreadPoolExecuter来尝试。这就是SO提出的类似问题的很多答案。无论如何,我无法让它发挥作用。

任何人都有想法,我可能做错了什么?

这是我的Logcat加载pdf的一部分,虽然我不认为这会有所帮助:

07-09 17:35:58.370  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ PdfView(0x1e000001).nativeCreateView(0x663138, 0x6d7e80)
07-09 17:35:58.370  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ Matrix: 0.147222 0.000000 0.000000 -0.148148 0.000000 80.000000
07-09 17:35:58.370  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ Viewbox: 0 0 106 80
07-09 17:35:58.370  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ doing the rendering...
07-09 17:35:58.490  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ Converting image buffer pixel order
07-09 17:35:58.490  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ PdfView.nativeCreateView() done
07-09 17:35:58.520  10080-10098/lp.german.bischofshofpresenter.app D/dalvikvm﹕ GC_FOR_ALLOC freed 3520K, 26% free 14324K/19207K, paused 21ms
07-09 17:35:58.530  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ PdfView(0x1ee00001).nativeCreateView(0x663138, 0x6d7e80)
07-09 17:35:58.530  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ Matrix: 1.066667 0.000000 0.000000 -0.948148 0.000000 256.000000
07-09 17:35:58.530  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ Viewbox: 0 0 256 256
07-09 17:35:58.530  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ doing the rendering...
07-09 17:35:58.750  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ Converting image buffer pixel order
07-09 17:35:58.760  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ PdfView.nativeCreateView() done
07-09 17:35:58.760  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ PdfView(0x1fc00001).nativeCreateView(0x663138, 0x6d7e80)
07-09 17:35:58.760  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ Matrix: 1.066667 0.000000 0.000000 -0.948148 -256.000000 256.000000
07-09 17:35:58.760  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ Viewbox: 0 0 256 256
07-09 17:35:58.760  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ doing the rendering...
07-09 17:35:58.990  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ Converting image buffer pixel order
07-09 17:35:58.990  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ PdfView.nativeCreateView() done
07-09 17:35:59.000  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ PdfView(0x20a00001).nativeCreateView(0x663138, 0x6d7e80)
07-09 17:35:59.000  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ Matrix: 1.066667 0.000000 0.000000 -0.948148 -256.000000 512.000000
07-09 17:35:59.000  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ Viewbox: 0 0 256 256
07-09 17:35:59.000  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ doing the rendering...
07-09 17:35:59.080  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ Converting image buffer pixel order
07-09 17:35:59.090  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ PdfView.nativeCreateView() done
07-09 17:35:59.090  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ PdfView(0x21800001).nativeCreateView(0x663138, 0x6d7e80)
07-09 17:35:59.090  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ Matrix: 1.066667 0.000000 0.000000 -0.948148 0.000000 512.000000
07-09 17:35:59.090  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ Viewbox: 0 0 256 256
07-09 17:35:59.090  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ doing the rendering...
07-09 17:35:59.260  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ Converting image buffer pixel order
07-09 17:35:59.260  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ PdfView.nativeCreateView() done
07-09 17:35:59.270  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ PdfView(0x22600001).nativeCreateView(0x663138, 0x6d7e80)
07-09 17:35:59.270  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ Matrix: 1.066667 0.000000 0.000000 -0.948148 -512.000061 256.000000
07-09 17:35:59.270  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ Viewbox: 0 0 256 256
07-09 17:35:59.270  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ doing the rendering...
07-09 17:35:59.330  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ Converting image buffer pixel order
07-09 17:35:59.330  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ PdfView.nativeCreateView() done
07-09 17:35:59.370  10080-10082/lp.german.bischofshofpresenter.app D/dalvikvm﹕ GC_CONCURRENT freed 3203K, 33% free 13048K/19207K, paused 2ms+4ms
07-09 17:35:59.370  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ PdfView(0x23400001).nativeCreateView(0x663138, 0x6d7e80)
07-09 17:35:59.370  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ Matrix: 1.066667 0.000000 0.000000 -0.948148 -512.000061 512.000000
07-09 17:35:59.370  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ Viewbox: 0 0 256 256
07-09 17:35:59.370  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ doing the rendering...
07-09 17:35:59.460  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ Converting image buffer pixel order
07-09 17:35:59.460  10080-10098/lp.german.bischofshofpresenter.app D/PdfDroid﹕ PdfView.nativeCreateView() done
07-09 17:36:05.700  10080-10080/lp.german.bischofshofpresenter.app D/OpenGLRenderer﹕ Flushing caches (mode 0)

1 个答案:

答案 0 :(得分:0)

似乎库不应该进入AsyncTask。该库可能已经是异步的,因为它允许您注册onLoad回调。您是否尝试过关注该库的自述文件中的示例?只需在主线程中运行调用(不使用AsyncTask),如果在加载完成后需要执行某些操作,请注册onLoad回调。