我有一个workking PDF下载器和PDFRenderer。我正在使用openFileInput方法来保护文件不受扫描程序的影响。我的问题是生成的Bitmap太小,无法读取。我尝试使用像推荐的每个例子,但我无法让它加载APK太多的logcats列表。这个版本没有。设备是Galaxy S6我只有AMD电脑所以没有模拟器这里和没有SDCard建议出于安全考虑我正在使用APP特定存储
PDFRenderer Bitmap的屏幕截图:
XML代码:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="net.madjobber.pdfdemo.PDFRender">
<ImageView
android:id="@+id/image"
android:layout_width="371dp"
android:layout_height="437dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintHorizontal_bias="0.0" />
<Button
android:id="@+id/previous"
android:layout_width="160dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="previous"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/image"
app:layout_constraintRight_toLeftOf="@+id/next"
android:layout_marginRight="8dp"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintHorizontal_bias="0.0" />
<Button
android:id="@+id/next"
android:layout_width="160dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="next"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="0dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/image"
app:layout_constraintVertical_bias="0.0" />
下载Java:
public class MainActivity extends AppCompatActivity {
Button btn;
String fileDownloadPath = "https://www.ets.org/Media/Tests/GRE/pdf/gre_research_validity_data.pdf";
String saveFilePath = "";
ProgressDialog dialog = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(MainActivity.this,"","File is Being Downloaded",true);
new Thread(new Runnable() {
@Override
public void run() {
downloadFile(fileDownloadPath,saveFilePath);
}
}).start();
}
});
}
public void downloadFile(String fileDownloadPath,String saveFilePath)
{
try {
URL u = new URL(fileDownloadPath);
URLConnection con = u.openConnection();
int lengthofContent=con.getContentLength();
DataInputStream DIStream=new DataInputStream(u.openStream());
byte[] buffer=new byte[lengthofContent];
DIStream.readFully(buffer);
DIStream.close();
DataOutputStream DOStream=new DataOutputStream(new FileOutputStream(getApplicationContext().getFilesDir() + "/" + "PDF.pdf"));
DOStream.write(buffer);
DOStream.flush();
DOStream.close();
hideProgressIndicator();
}
catch (FileNotFoundException e){
hideProgressIndicator();
}
catch (IOException e) {
}
catch (Exception e){
}
}
private void hideProgressIndicator() {
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.dismiss();
}
});
}
public void PDFRenderer (View view) {
Intent intent = new Intent(MainActivity.this, PDFRender.class);
startActivity(intent);
}
}
PDFRenderer JAVA:
public class PDFRender extends AppCompatActivity {
private ImageView imageView;
private int currentPage = 0;
private Button next,previous;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdfrender);
next =(Button) findViewById(R.id.next);
previous = (Button) findViewById(R.id.previous);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentPage++;
render();
}
});
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentPage--;
render();
}
});
render();
}
private void render() {
//open PDF from internal storage and pass the variable to renderer
String Path = getApplicationContext().getFilesDir().getPath();
File file = new File( Path +"/PDF.pdf");
try{
imageView = (ImageView) findViewById(R.id.image);
int REQ_WIDTH = imageView.getWidth();
int REQ_HEIGHT = imageView.getHeight();
Bitmap bitmap = Bitmap.createBitmap(REQ_WIDTH, REQ_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 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);
imageView.setImageMatrix(m);
imageView.setImageBitmap(bitmap);
imageView.invalidate();
}catch (Exception e) {
e.printStackTrace();
}
}
任何帮助都将非常感谢,即使有骗子眼镜,我也无法阅读PDF。提前谢谢!!