following is my PDFRenderer code and error is "File not found" Kindly help..
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();
}
}
}
You don't read your file in the right way. Here's how to read a file located in the asset folder:
See the official example for more help.