Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
629 views
in Technique[技术] by (71.8m points)

android - "KOTLIN" Saving PDF to Media.Downloads creates pdf with 0 Byte

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()
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...