我试过了:
package com.example.dell.pdfreader;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.pdf.PdfRenderer;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class MainActivity extends AppCompatActivity {
private ImageView img;
private int currentpage = 0;
private Button next, previous;
File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
next = (Button) findViewById(R.id.next);
previous = (Button) findViewById(R.id.previous);
// file=getFile();
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentpage++;
render(file);
}
});
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentpage--;
render(file);
}
});
}
private void render(File file) {
try {
img = (ImageView) findViewById(R.id.image);
int width = img.getWidth();
int height = img.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY));
if (currentpage < 0) {
currentpage = 0;
} else if (currentpage > renderer.getPageCount()) {
currentpage = renderer.getPageCount() - 1;
Matrix matrix = img.getImageMatrix();
Rect rect = new Rect(0, 0, width, height);
renderer.openPage(currentpage).render(bitmap, rect, matrix, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
img.setImageMatrix(matrix);
img.setImageBitmap(bitmap);
img.invalidate();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private File getFile() {
File file=new File(this.getExternalFilesDir(null),"Example.pdf");;
byte buffer[]=null;
OutputStream output = null;
InputStream input = null;
try {
input = this.getAssets().open("Example.pdf");
buffer = new byte[input.available()];
input.read(buffer);
output = new FileOutputStream(file);
output.write(buffer);
}
catch(FileNotFoundException e){
e.printStackTrace();
Toast.makeText(this,"File not Found",Toast.LENGTH_LONG).show();
return null;
}
catch (IOException e) {
e.printStackTrace();
Toast.makeText(this,"Failed to open",Toast.LENGTH_LONG).show();
return null;
}
return file;
}
}
以上代码生成以下错误:
> java.lang.NullPointerException: Attempt to invoke virtual method
> 'java.lang.String java.io.File.getPath()' on a null object reference
> 03-18 15:42:16.245 6826-6826/com.example.dell.pdfreader W/System.err:
> at
> android.os.ParcelFileDescriptor.openInternal(ParcelFileDescriptor.java:269)
> 03-18 15:42:16.245 6826-6826/com.example.dell.pdfreader W/System.err:
> at android.os.ParcelFileDescriptor.open(ParcelFileDescriptor.java:200)
> 03-18 15:42:16.246 6826-6826/com.example.dell.pdfreader W/System.err:
> at
> com.example.dell.pdfreader.MainActivity.render(MainActivity.java:76)
> 03-18 15:42:16.246 6826-6826/com.example.dell.pdfreader W/System.err:
> at
> com.example.dell.pdfreader.MainActivity.access$100(MainActivity.java:26)
> 03-18 15:42:16.246 6826-6826/com.example.dell.pdfreader W/System.err:
> at
> com.example.dell.pdfreader.MainActivity$1.onClick(MainActivity.java:47)
> 03-18 15:42:16.246 6826-6826/com.example.dell.pdfreader W/System.err:
> at android.view.View.performClick(View.java:5265) 03-18 15:42:16.246
> 6826-6826/com.example.dell.pdfreader W/System.err: at
> android.view.View$PerformClick.run(View.java:21534) 03-18 15:42:16.246
> 6826-6826/com.example.dell.pdfreader W/System.err: at
> android.os.Handler.handleCallback(Handler.java:815) 03-18 15:42:16.247
> 6826-6826/com.example.dell.pdfreader W/System.err: at
> android.os.Handler.dispatchMessage(Handler.java:104) 03-18
> 15:42:16.247 6826-6826/com.example.dell.pdfreader W/System.err: at
> android.os.Looper.loop(Looper.java:207) 03-18 15:42:16.247
> 6826-6826/com.example.dell.pdfreader W/System.err: at
> android.app.ActivityThread.main(ActivityThread.java:5728) 03-18
> 15:42:16.247 6826-6826/com.example.dell.pdfreader W/System.err: at
> java.lang.reflect.Method.invoke(Native Method) 03-18 15:42:16.247
> 6826-6826/com.example.dell.pdfreader W/System.err: at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
> 03-18 15:42:16.247 6826-6826/com.example.dell.pdfreader W/System.err:
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
答案 0 :(得分:0)
首先,正如评论中所述,file
是null
,因为您从未在其上设置过值。
其次,对于资产中的PDF,没有文件。使用openFd()
上的AssetManager
获取资产上的AssetFileDescriptor
,然后致电getParcelFileDescriptor()
上的AssetFileDescriptor
获取ParcelFileDescriptor
。
第三,PdfRenderer
不处理任意PDF文件。它只用于打印预览,仅此而已。最有可能的是,您需要使用here。