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
file == null。你从未给它一个价值。你从来没有初始化它。你从来没有为它分配一个File对象。 –
+0
你永远不会调用getFile()。 –
+0
'output.write(buffer);'。好。但添加output.close();. –