Home > OS >  ACTION_CREATE_DOCUMENT write a pdf file from url response, android?
ACTION_CREATE_DOCUMENT write a pdf file from url response, android?

Time:11-23

I am using shared storage to write a pdf file. The file is created but it is of 0 bytes. Not able to understand what is the issue.

Below is my code

  • ACTION_CREATE_DOCUMENT

    fun saveFile() {
    
     val exportIntent = Intent(Intent.ACTION_CREATE_DOCUMENT)
     exportIntent.addCategory(Intent.CATEGORY_OPENABLE)
     exportIntent.type = "application/pdf"
     val filename = "test.pdf"
     exportIntent.putExtra(Intent.EXTRA_TITLE, filename)
     startForResult.launch(exportIntent)
    }
    
  • Activity result

    private val MEGABYTE = 1024 * 1024
    
    val startForResult =
     registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
         if (result.resultCode == Activity.RESULT_OK) {
             val intent = result.data
             if (intent?.data != null) {
                 val uri: Uri = intent.data!!
                 GlobalScope.launch {
                     writeFile(uri)
                 }
    
    
             }
         }
     }
    
  • Write file method

     fun writeFile(uri:Uri){
     val outputStream: OutputStream? =
         requireContext().contentResolver.openOutputStream(uri)
     if (outputStream != null) {
         val url = URL(some url)
         val urlConnection: HttpURLConnection =
             url.openConnection() as HttpURLConnection
         urlConnection.setRequestMethod("GET")       
         urlConnection.setDoOutput(true)
         urlConnection.connect()
    
         val inputStream: InputStream = urlConnection.getInputStream()
    
         val buffer = ByteArray(MEGABYTE)
         var bufferLength = 0
         while (inputStream.read(buffer).also { bufferLength = it } > 0) {
             outputStream.write(buffer, 0, bufferLength)
         }
    
         outputStream.close()
     }
     }
    

CodePudding user response:

Thanks to comment from @blackapps was able to work it out Removed the following

urlConnection.setRequestMethod("GET")
urlConnection.setDoOutput(true)

Correct function becomes

fun writeFile(uri: Uri, docUrl: String) {
    val outputStream: OutputStream? =
        requireContext().contentResolver.openOutputStream(uri)
    if (outputStream != null) {
        val url =
            URL(docUrl)
        val urlConnection: HttpURLConnection =
            url.openConnection() as HttpURLConnection
        urlConnection.connect()

        val inputStream: InputStream = urlConnection.getInputStream()
        val buffer = ByteArray(1024 * 1024)
        var bufferLength = 0
        while (inputStream.read(buffer).also { bufferLength = it } > 0) {
            outputStream.write(buffer, 0, bufferLength)
        }
        outputStream.close()
    }
}

CodePudding user response:

Check inputstream is accessible or not by logging inputStream.available() Must be >0

The use this logic to write inputstream to outputstream

byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
    outputStream.write(buffer, 0, len);
}
outputStream.close()
  • Related