android-pdfview
android-pdfview copied to clipboard
Fix PDF File corrupted exception
Hi this fix prevent the lib to crash on RuntimeException File corrupted. it's a basic try - catch that set cancelled to true, to cancel the process.
here is the full trace:
java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:300) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) at java.util.concurrent.FutureTask.setException(FutureTask.java:222) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818) Caused by: java.lang.RuntimeException: PDF file is corrupted at org.vudroid.pdfdroid.codec.PdfDocument.open(PdfDocument.java) at org.vudroid.pdfdroid.codec.PdfDocument.openDocument(PdfDocument.java:28) at org.vudroid.pdfdroid.codec.PdfContext.openDocument(PdfContext.java:18) at org.vudroid.core.DecodeServiceBase.open(DecodeServiceBase.java:59) at com.joanzapata.pdfview.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:52) at com.joanzapata.pdfview.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:31) at android.os.AsyncTask$2.call(AsyncTask.java:288) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818)
@JoanZapata is there any plan to merge this pull request? I really need that handle without import library explicity into the project cc @sonique6784
👍
@sonique6784 thanks for your solution.
It will be better to add showing message for user.
@Override
protected Void doInBackground(Void... params) {
try {
decodeService = new DecodeServiceBase(new PdfContext());
decodeService.setContentResolver(pdfView.getContext().getContentResolver());
decodeService.open(uri);
} catch (Exception e){
// Prevent java.lang.RuntimeException: PDF file is corrupted
this.cancelled = true;
showErrorMessage(e.getMessage());
}
return null;
}
private void showErrorMessage(final String message) {
Handler handler = new Handler(pdfView.getContext().getMainLooper());
handler.post( new Runnable(){
public void run(){
Toast.makeText(pdfView.getContext(), message, Toast.LENGTH_LONG).show();
}
});
}