PDFRenderer文件未找到异常

时间:2014-12-08 11:55:47

标签: android pdfrenderer

以下是我的PDFRenderer代码,错误是"找不到文件" 请帮助..

import java.io.File;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.pdf.PdfRenderer;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
//import com.sun.pdfview.PDFRenderer;


public class MainActivity extends Activity 
 {
    private ImageView imageView;
    private int currentPage = 0;
    private Button next, previous;
    int REQ_WIDTH = 1;
    int REQ_HEIGHT = 1;
    File file = new File("/assets/sample.pdf");
    private ParcelFileDescriptor mFileDescriptor;
    Context context=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.imageView);

        REQ_WIDTH = imageView.getWidth();
        REQ_HEIGHT = imageView.getHeight();

        next = (Button) findViewById(R.id.next);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("TAG -  in next click"," TAG - in next click");               
                Log.i("TAG - BEFORE CLICK ","currentPage - "+currentPage);
                currentPage++;
                render();/**/
                Log.i("TAG - POST CLICK ","currentPage - "+currentPage);
            }
        });

        previous = (Button) findViewById(R.id.previous);
        previous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("TAG -  in prevoius click","TAG - in prevoius click");
                Log.i("TAG - BEFORE CLICK ","currentPage - "+currentPage);
                currentPage--;
                render();
                Log.i("TAG - POST CLICK ","currentPage - "+currentPage);
            }
        });
       render();        
    }

    private void render() {/**/
        try {       
            Log.e("TAG - In render()"," TAG - In render()");            
            imageView = (ImageView) findViewById(R.id.imageView);

            REQ_WIDTH = imageView.getWidth();
            REQ_HEIGHT = imageView.getHeight();

            Log.e("TAG - In render2()","TAG - In render2");
            System.out.println("REQ_HEIGHT - "+REQ_HEIGHT +" "+"REQ_WIDTH - "+REQ_WIDTH);

            Bitmap bitmap = Bitmap.createBitmap(REQ_WIDTH, REQ_HEIGHT, Bitmap.Config.ARGB_4444);           

            //ParcelFileDescriptor filedesc=ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
            //renderer = new PDFRenderer(getSeekableFileDescriptor(filedesc));

            try{
                mFileDescriptor = context.getAssets().openFd("file:///assets/sample.pdf").getParcelFileDescriptor();/**/
                }catch(Exception ae)
                {
                    Log.e("TAG - In render3()"," File descriptor empty");
                    ae.printStackTrace();
                }

                // This is the PdfRenderer we use to render the PDF.
                PdfRenderer renderer = new PdfRenderer(mFileDescriptor);
                //PDFRenderer renderer = new PDFRenderer(getSeekableFileDescriptor(mFileDescriptor));

            if(mFileDescriptor != null)
            {
                Log.i("mFileDescriptor not null","check");
            }
            else 
            {
                Log.i("mFileDescriptor is null","check");
            }

            final int pageCount = renderer.getPageCount();            
            Log.i("Total pages - "+pageCount,"currentPage -  "+currentPage);

            if (currentPage < 0) {
                    currentPage = 0;
            } else if (currentPage >= renderer.getPageCount()) {
                    currentPage = renderer.getPageCount() - 1;
            }

            Matrix m = imageView.getImageMatrix();
            Rect rect = new Rect(0, 0, REQ_WIDTH, REQ_HEIGHT);
            renderer.openPage(currentPage).render(bitmap, rect, m, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
            if(m!=null)
            {
                imageView.setImageMatrix(m);
                imageView.setImageBitmap(bitmap);
                imageView.invalidate();
                renderer.close();
            }else
            {
                Log.i("TAG - ","TAG - ");
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        }       
}

2 个答案:

答案 0 :(得分:0)

从PDFRenderer.class的源代码:

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
...

Link to full source code

AWT在Android上非常有限。你必须找另一个pdf库的人。

答案 1 :(得分:0)

您没有以正确的方式阅读文件。以下是如何读取资产文件夹中的文件:

mFileDescriptor = context.getAssets().openFd("sample.pdf").getParcelFileDescriptor();

请参阅official example以获取更多帮助。