Search code examples
androidkotlinandroid-studio

permission for saving pdf Android studio


i created a simple app that generated a pdf with 2 pages now i need to share the pdf and also save it in the device from what i already know it requires permissions to do that.

i am new to this and dont know what to do so i watched some tutorials and read some google articles and found out that the permission stuff and storage access is change multiple times through out android 9-13 my targeted sdk versions are 29-34.

how can i manage all the changes and ask for permissions? also i cant find any permission that says for non media files. so do i even need permission or not?

EDIT: According to this Table i dont need permission to store files that are not photo, video or audio Data and File Storage Overview

so i can store them in cache directory let the user share and delete it afterwards


Solution

  • This code will help you out and five idea for storing the pdf

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // To Download File for Android 10 and above
                val content = ContentValues().apply {
                    put(MediaStore.MediaColumns.DISPLAY_NAME, fileName)
                    put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS + "/ZipApp/")
                }
                val uri = context.contentResolver.insert(
                    MediaStore.Downloads.EXTERNAL_CONTENT_URI,
                    content
                )
                uri?.apply {
                    val responseBody = getResponseBody(url)
                    if (responseBody != null) {
                        responseBody.byteStream().use { inputStream ->
                            context.contentResolver.openOutputStream(uri)?.use { fileOutStream ->
                                writeOutStream(
                                    inStream = inputStream,
                                    outStream = fileOutStream,
                                    contentLength = responseBody.contentLength(),
                                    emitter = emitter
                                )
                            }
                            emitter.onComplete()
                        }
                    } else {
                        emitter.onError(Throwable(errorMessage))
                    }
                }
            } else { // For Android versions below than 10
                val directory = File(
                    Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_DOWNLOADS).absolutePath + "/ZipApp/"
                ).apply {
                    if (!exists()) {
                        mkdir()
                    }
                }
                val file = File(directory, fileName)
                val responseBody = getResponseBody(url)
    
                if (responseBody != null) {
                    responseBody.byteStream().use { inputStream ->
                        file.outputStream().use { fileOutStream ->
                            writeOutStream(
                                inStream = inputStream,
                                outStream = fileOutStream,
                                contentLength = responseBody.contentLength(),
                                emitter = emitter
                            )
                        }
                        emitter.onComplete()
                    }
                } else {
                    emitter.onError(Throwable(errorMessage))
                }
            }