PdfRenderer 错误:java.io.IOException:无法创建文档.错误:3



我正在尝试将PDF转换为图像位图..为此,我在下面使用此代码。

pdfFile = Environment.getExternalStorageDirectory().toString() + "/" + "imagepdf.pdf"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try {
Uri uri = MainActivity.getUriFromFile(PdfExtrectorActivity.this,new File(pdfFile));
final ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(
uri, "r");
final FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
pdfRenderer = new PdfRenderer(parcelFileDescriptor);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap;
final int pageCount = pdfRenderer.getPageCount();
for (int i = 0; i < pageCount; i++) {
PdfRenderer.Page page = pdfRenderer.openPage(i);

bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);

// close the page
page.close();
}
// close the renderer
pdfRenderer.close();
}

当我将包裹文件描述符发送到pdfRenderer时,它会抛出异常

java.io.IOException: cannot create document. Error: 3

我从 GitHub 问题中找到了一些链接。 https://github.com/googlesamples/android-PdfRendererBasic/issues/15 但这对我无济于事。

接受任何帮助。 谢谢。

我的问题解决了,使用此解决方案从SD卡中的PDF转换图像位图。

这对我有用

File file = new File(getCacheDir(), PDF_FILE_NAME);
//if (!file.exists()) {
// Since PdfRenderer cannot handle the compressed asset file directly, we copy it into
// the cache directory.
//InputStream asset = context.getAssets().open("imagepdf.pdf");
FileInputStream fileInputStream = null;
FileOutputStream output = null;
try {
fileInputStream = new FileInputStream( Environment.getExternalStorageDirectory().toString() +  "/imagepdf.pdf");
output = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
final byte[] buffer = new byte[1024];
int size;
while ((size = fileInputStream.read(buffer)) != -1) {
output.write(buffer, 0, size);
}
fileInputStream.close();
output.close();
//}
ParcelFileDescriptor mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
// This is the PdfRenderer we use to render the PDF.
PdfRenderer renderer = null;
if (mFileDescriptor != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
renderer = new PdfRenderer(mFileDescriptor);
Bitmap bitmap;
final int pageCount = renderer.getPageCount();
for (int i = 0; i < pageCount; i++) {
PdfRenderer.Page page = renderer.openPage(i);
// say we render for showing on the screen
bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
myImageBitmap.add(bitmap);
// do stuff with the bitmap
// close the page
page.close();
}
// close the renderer
renderer.close();
}
}

相关内容

  • 没有找到相关文章

最新更新