我在 AsyncTask 中遇到了一些奇怪的行为。我正在尝试将 pdf 异步加载到 PDFView (我使用了这个库)
这是我的代码:
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。但是从那里它被卡住并且永远加载和加载。
我检查了正在运行的线程,发现 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)