以下是我的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();
}
}
}
您没有以正确的方式读取文件。以下是读取位于资产文件夹中的文件的方法:
mFileDescriptor = context.getAssets().openFd("sample.pdf").getParcelFileDescriptor();
有关更多帮助,请参阅官方示例。
来自 PDFRenderer 的源代码.class:
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
...
链接到完整的源代码
AWT在Android afaik上非常有限。你将不得不寻找另一个pdf图书馆的人。