从pdf转换为位图

时间:2015-04-16 07:57:09

标签: android pdf bitmap

我尝试使用pdfviewer.jar lib将pdf文件转换为Bitmap,然后转换时出错。我的错误日志是

04-16 13:05:19.232: E/dalvikvm-heap(14882): Out of memory on a 959056-byte allocation.
04-16 13:05:19.235: E/Error(14882): null
04-16 13:05:19.235: E/Error(14882): java.lang.OutOfMemoryError
04-16 13:05:19.235: E/Error(14882):     at android.graphics.Bitmap.nativeCreate(Native Method)
04-16 13:05:19.235: E/Error(14882):     at android.graphics.Bitmap.createBitmap(Bitmap.java:836)
04-16 13:05:19.235: E/Error(14882):     at android.graphics.Bitmap.createBitmap(Bitmap.java:813)
04-16 13:05:19.235: E/Error(14882):     at android.graphics.Bitmap.createBitmap(Bitmap.java:780)
04-16 13:05:19.235: E/Error(14882):     at com.sun.pdfview.PDFPage.getImage(PDFPage.java:219)
04-16 13:05:19.235: E/Error(14882):     at com.praylatest.controller.PDFPage$PdfFind.showPage(PDFPage.java:122)
04-16 13:05:19.235: E/Error(14882):     at com.praylatest.controller.PDFPage$PdfFind.access$3(PDFPage.java:116)
04-16 13:05:19.235: E/Error(14882):     at com.praylatest.controller.PDFPage.<init>(PDFPage.java:40)
04-16 13:05:19.235: E/Error(14882):     at com.praylatest.activities.ViewPagerAdapter$ContactWebserviceTask.doInBackground(ViewPagerAdapter.java:696)
04-16 13:05:19.235: E/Error(14882):     at com.praylatest.activities.ViewPagerAdapter$ContactWebserviceTask.doInBackground(ViewPagerAdapter.java:1)
04-16 13:05:19.235: E/Error(14882):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-16 13:05:19.235: E/Error(14882):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-16 13:05:19.235: E/Error(14882):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-16 13:05:19.235: E/Error(14882):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-16 13:05:19.235: E/Error(14882):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-16 13:05:19.235: E/Error(14882):     at java.lang.Thread.run(Thread.java:841)

我的PDFPage类代码是

    public class PDFPage {

    private static ArrayList<Bitmap> bArr;
    int Ncount = 0;
    PdfFind obj;
    Context context;

    public PDFPage(Context ctx, String strPdfName) {
        context = ctx;
        bArr = new ArrayList<Bitmap>();
        obj = new PdfFind();
        try {
            obj.parsePDF(strPdfName);
            if(obj.mPdfFile != null){
                Ncount = obj.mPdfFile.getNumPages();
                if(obj.mPdfFile.getNumPages() > 0){
                    for (int i = 1; i <= obj.mPdfFile.getNumPages(); i++) {
                        Bitmap bmp = obj.showPage(i);
                        bArr.add(bmp);
                    }
                }
            }
        } catch (PDFAuthenticationFailureException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public ArrayList<Bitmap> LoadMore(Context ctx, String strPdfName, ArrayList<Bitmap> arr){
        try {
            obj.parsePDF(strPdfName);
            if(obj.mPdfFile != null && arr.size() < Ncount){
                if(obj.mPdfFile.getNumPages() > 0){
                    for (int i = (arr.size() + 1); i <= obj.mPdfFile.getNumPages(); i++) {
                        Bitmap bmp = obj.showPage(i);
                        arr.add(bmp);
                    }
                }
            }
        } catch (PDFAuthenticationFailureException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return arr;
    }

    public ArrayList<Bitmap> getArrBitmapImages() {
            return bArr;
    }

    private class PdfFind {
        private PDFFile mPdfFile;
        private com.sun.pdfview.PDFPage mPdfPage;
        private void parsePDF(String filename)
        throws PDFAuthenticationFailureException {

            try {
                File f = new File(filename);
                long len = f.length();
                if (len == 0) {
                    Log.i("No File","File length is 0");
                } else {
                    openFile(f);
                }
            } catch (PDFAuthenticationFailureException e) {
                throw e;
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }

        public void openFile(File file) throws IOException {
            // first open the file for random access
            RandomAccessFile raf = new RandomAccessFile(file, "r");

            // extract a file channel
            FileChannel channel = raf.getChannel();

            // now memory-map a byte-buffer
            ByteBuffer bb = ByteBuffer.NEW(channel.map(
                    FileChannel.MapMode.READ_ONLY, 0, channel.size()));
            // create a PDFFile from the data                      
            mPdfFile = new PDFFile(bb);
        }

        private Bitmap showPage(int page) throws Exception {
            try {
                System.gc();

                mPdfPage = mPdfFile.getPage(page, true);
                RectF clip = null;
                Bitmap bi = mPdfPage.getImage((Constants.SCREEN_WIDTH),
                                (Constants.SCREEN_HEIGHT), clip, true, true);                        
                return bi;
            } catch (Throwable e) {
                Log.e("Error", e.getMessage(), e);
            }
            return null;
        }
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return Ncount;
    }
 }

如果我尝试转换超过10个pdf文件,那么它会显示错误。请帮帮我。

0 个答案:

没有答案