Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
628 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...