I am trying to save a pdf that I created in-app programmatically to a specified directory in Downloads. Everything is working except the pdf size. It creates a pdf with 0 bytes and it's impossible to open it.
Here is my code (Sorry, i am a beginner):
val document = PdfDocument()
val pageInfo = PageInfo.Builder(1080, 1920, 1).create()
val page = document.startPage(pageInfo)
val canvas = page.canvas
val paint = Paint()
paint.color = Color.RED
canvas.drawText("Hallo", 80f, 50f, paint)
document.finishPage(page)
val resolver = contentResolver
val values = ContentValues()
values.put(MediaStore.MediaColumns.DISPLAY_NAME, "test")
values.put(MediaStore.MediaColumns.MIME_TYPE, "application/pdf")
values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS + "/" + "xyz")
val uri = resolver.insert(MediaStore.Files.getContentUri("external"), values)
// You can use this outputStream to write whatever file you want:
val outputStream = resolver.openOutputStream(uri!!)
document.close()
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…