Commit 4592181e authored by wanglei's avatar wanglei

...

parent ef7a5af2
...@@ -29,7 +29,9 @@ object PdfBoxUtils { ...@@ -29,7 +29,9 @@ object PdfBoxUtils {
fun getNumberOfPages(filePath: String, password: String? = null, uri: String? = null): Int { fun getNumberOfPages(filePath: String, password: String? = null, uri: String? = null): Int {
val document = val document =
loadPDDocument(path = filePath, password, uri) loadPDDocument(path = filePath, password, uri)
return document.numberOfPages val number = document.numberOfPages
document.close()
return number
} }
...@@ -43,10 +45,9 @@ object PdfBoxUtils { ...@@ -43,10 +45,9 @@ object PdfBoxUtils {
): Drawable { ): Drawable {
val document = loadPDDocument(filePath, password, uri) val document = loadPDDocument(filePath, password, uri)
val renderer = PDFRenderer(document) val renderer = PDFRenderer(document)
val bitmap: Bitmap = renderer.renderImage(pageIndex, scale, ImageType.RGB, RenderDestination.EXPORT) val bitmap: Bitmap = renderer.renderImage(pageIndex, scale, ImageType.RGB, RenderDestination.EXPORT)
val drawable = BitmapDrawable(context.resources, bitmap) val drawable = BitmapDrawable(context.resources, bitmap)
document.close()
return drawable return drawable
} }
...@@ -143,8 +144,9 @@ object PdfBoxUtils { ...@@ -143,8 +144,9 @@ object PdfBoxUtils {
password: String, password: String,
uri: String? = null uri: String? = null
): Boolean { ): Boolean {
var pdfDocument: PDDocument? = null
try { try {
val pdfDocument = loadPDDocument(filePath, password, uri) pdfDocument = loadPDDocument(filePath, password, uri)
// 尝试使用提供的密码加载PDF文件 // 尝试使用提供的密码加载PDF文件
pdfDocument.use { document -> pdfDocument.use { document ->
// 如果没有抛出异常,说明密码正确 // 如果没有抛出异常,说明密码正确
...@@ -156,10 +158,15 @@ object PdfBoxUtils { ...@@ -156,10 +158,15 @@ object PdfBoxUtils {
} catch (e: IOException) { } catch (e: IOException) {
// 其他I/O异常处理 // 其他I/O异常处理
println("An I/O error occurred: " + e.message) println("An I/O error occurred: " + e.message)
} finally {
pdfDocument?.close()
} }
return false return false
} }
/**
* 注意一下PDDocument用完都要关闭一下
*/
private fun loadPDDocument(path: String, password: String?, uri: String? = null): PDDocument { private fun loadPDDocument(path: String, password: String?, uri: String? = null): PDDocument {
return if (uri == null) { return if (uri == null) {
if (password == null) { if (password == null) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment