获取UnsupportedOperationException:不显示在Android中将PDF转换为图像的图像

时间:2013-09-22 12:04:35

标签: android pdf

我想以图像格式获得第一个pdf页面,我正在使用PdfViewer.jar,这是我的代码

private void pdfLoadImages()
{
    try
    {
        // run async
        new AsyncTask<Void, Void, Void>()
                {
                    // create and show a progress dialog
                    ProgressDialog progressDialog = ProgressDialog.show(getActivity(), "", "Opening...");

                    @Override
                    protected void onPostExecute(Void result)
                    {
                        //after async close progress dialog
                        progressDialog.dismiss();
                    }

                    @Override
                    protected Void doInBackground(Void... params)
                    {
                        try
                        {
                            // select a document and get bytes
                            File file = new File(librosPdfRuta[0]);
                            RandomAccessFile raf = new RandomAccessFile(file, "r");
                            FileChannel channel = raf.getChannel();
                            ByteBuffer bb = ByteBuffer.NEW(channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()));
                            raf.close();
                            // create a pdf doc
                            PDFFile pdf = new PDFFile(bb);
                            //Get the first page from the pdf doc
                            PDFPage PDFpage = pdf.getPage(1, true);
                            //create a scaling value according to the WebView Width
                            //final float scale = 1000 / PDFpage.getWidth() * 0.95f;
                            //convert the page into a bitmap with a scaling value
                            float scale = 1;
                            Bitmap page = PDFpage.getImage((int)(PDFpage.getWidth() * scale), (int)(PDFpage.getHeight() * scale), null, true, true);

                            guardarBytes(getBytes(page));


                    }
                    catch (Exception e)
                    {
                        Log.d("CounterA", e.toString());
                    }
                        return null;
                    }
                }.execute();
                System.gc();// run GC
    }
    catch (Exception e)
    {
        Log.d("error", e.toString());
    }
}

我从Need help to convert a Pdf page into Bitmap in Android Java

获取此代码

但我总是得到这个错误

09-22 13:51:37.232: E/APV.PDFRenderer(30616): do not show images
09-22 13:51:37.232: E/APV.PDFRenderer(30616): java.lang.UnsupportedOperationException: do not show images
09-22 13:51:37.232: E/APV.PDFRenderer(30616):   at com.sun.pdfview.PDFImage.getImage(PDFImage.java:223)
09-22 13:51:37.232: E/APV.PDFRenderer(30616):   at com.sun.pdfview.PDFRenderer.drawImage(PDFRenderer.java:374)
09-22 13:51:37.232: E/APV.PDFRenderer(30616):   at com.sun.pdfview.PDFImageCmd.execute(PDFPage.java:654)
09-22 13:51:37.232: E/APV.PDFRenderer(30616):   at com.sun.pdfview.PDFRenderer.iterate(PDFRenderer.java:674)
09-22 13:51:37.232: E/APV.PDFRenderer(30616):   at com.sun.pdfview.BaseWatchable.run(BaseWatchable.java:101)
09-22 13:51:37.232: E/APV.PDFRenderer(30616):   at com.sun.pdfview.BaseWatchable.execute(BaseWatchable.java:263)
09-22 13:51:37.232: E/APV.PDFRenderer(30616):   at com.sun.pdfview.BaseWatchable.go(BaseWatchable.java:197)
09-22 13:51:37.232: E/APV.PDFRenderer(30616):   at com.sun.pdfview.PDFPage.getImage(PDFPage.java:237)
09-22 13:51:37.232: E/APV.PDFRenderer(30616):   at com.bookreader.LibrosDispositivo$1.doInBackground(LibrosDispositivo.java:347)
09-22 13:51:37.232: E/APV.PDFRenderer(30616):   at com.bookreader.LibrosDispositivo$1.doInBackground(LibrosDispositivo.java:1)
09-22 13:51:37.232: E/APV.PDFRenderer(30616):   at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-22 13:51:37.232: E/APV.PDFRenderer(30616):   at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-22 13:51:37.232: E/APV.PDFRenderer(30616):   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-22 13:51:37.232: E/APV.PDFRenderer(30616):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-22 13:51:37.232: E/APV.PDFRenderer(30616):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-22 13:51:37.232: E/APV.PDFRenderer(30616):   at java.lang.Thread.run(Thread.java:841)

我该如何解决?非常感谢你

1 个答案:

答案 0 :(得分:-1)

AsyncTask由3个参数组成。在你的情况下 android.os.AsyncTask。 Params和Result是从ui线程输入和输出的。 你不应该在doInBackground()方法中使用任何使用ui线程的函数。 尝试从doInBachround()和onPostExecute中返回位图,然后将其删除。