美好的一天,我在android studio>上几乎是pdf渲染器的新手。 api 21,我发现了一个简单的代码来读取assets文件夹上的pdf。 我的问题是,当它加载时,文件的第一页没有显示,除非我按下一个按钮。我的申请旨在阅读用户手册pdf。
username error: Username must be at least 6 charachers long
password error: Password msut be at least 8 charachers long
答案 0 :(得分:1)
REQ_WIDTH和REQ_HEIGHT为0.尝试添加与屏幕尺寸相关的值
答案 1 :(得分:0)
你可以尝试这个代码我只是稍微清理了一下
import android.app.AlertDialog;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.pdf.PdfRenderer;
import android.os.ParcelFileDescriptor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class UsersManual extends AppCompatActivity {
private ImageView imageView;
private int currentPage = 0;
private Button next, previous;
private PdfRenderer renderer;
int REQ_WIDTH = imageView.getWidth();
int REQ_HEIGHT = imageView.getHeight();
private int count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_users_manual);
next = (Button) findViewById(R.id.next);
previous = (Button) findViewById(R.id.previous);
render();
display();
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentPage++;
display();
}
});
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentPage--;
display();
}
});
}
private void render() {
try {
imageView = (ImageView) findViewById(R.id.image);
REQ_WIDTH = imageView.getWidth();
REQ_HEIGHT = imageView.getHeight();
File f = new File(getFilesDir() + "/Learning Python 5th Ed 2013.pdf");
renderer = new PdfRenderer(ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY));
count = renderer.getPageCount();
} catch (Exception e) {
e.printStackTrace();
}
}
private void display(){
if (currentPage <= 0) {
currentPage = 0;
} else if (currentPage > count) {
currentPage = renderer.getPageCount() - 1;
}
Bitmap bitmap = Bitmap.createBitmap(REQ_WIDTH, REQ_HEIGHT, Bitmap.Config.ARGB_8888);
Matrix m = imageView.getImageMatrix();
Rect rect = new Rect(0, 0, REQ_WIDTH, REQ_HEIGHT);
renderer.openPage(currentPage).render(bitmap, rect, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
imageView.setImageMatrix(m);
imageView.setImageBitmap(bitmap);
imageView.invalidate();
}
}