I recently developed an app which downloads pdfs from the server and saves them in a local storage.
I can open the file if I don’t uninstall the app. If I reinstall the app then I cannot open the file.
Also I don’t need access to other folders as I only target a folder ( folder That will be created ) in downloads folder.
I tried using various methods like mediastore or something new methods but cannot solve the issue.
fun openPdfIntent(filePath: File, context: Context) {
Log.i("Pdf", "openPdfIntent: ${filePath.absolutePath} ")
val pdfIntent = Intent(Intent.ACTION_VIEW)
val path =
FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", filePath)
pdfIntent.setDataAndType(
path,
"application/pdf"
)
pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
try {
if (filePath.canRead()) {
Log.i("Pdf", "openPdfIntent: exists and open")
context.startActivity(pdfIntent)
} else {
pdfIntent.setDataAndType(
filePath.toUri(),
"application/pdf"
)
pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
Log.i("Pdf", "openPdfIntent: Doesn't exists ")
}
} catch (e: Exception) {
Toast.makeText(context, "Please Install Pdf Viewer", Toast.LENGTH_SHORT).show()
if (filePath.exists()) {
filePath.delete()
Log.i("Pdf", "openPdfIntent: Deleted ")
}
}
}
//function to download files.
fun onDownload(
index: Int,
url: String,
unitName: String,
subjectName: String,
context: Context,
) {
viewModelScope.launch(IO) {
val kDownloader = KDownloader.create(context)
withContext(
IO
) {
this.async {
val req = kDownloader.newRequestBuilder(
url,
getFileDirectory(context).path + "/KIET PDFS/$subjectName",
"${subjectName}_${unitName}.pdf"
).build()
kDownloader.enqueue(req,
onStart = {
Log.i("Downloads", "onDownload: Started for file $unitName.pdf")
downloadStatus[index] = downloadStatus[index].copy(
downloadProgress = DownloadProgress.Running
)
},
onCompleted = {
Log.i("Downloads", "onDownload: Success for $unitName.pdf ")
downloadStatus[index] = downloadStatus[index].copy(
downloadProgress = DownloadProgress.Success
)
}, onError = {
Log.i("Downloads", "onDownload: Error $it")
downloadStatus[
index
] = downloadStatus[index].copy(
downloadProgress = DownloadProgress.Failed
)
}
)
}.await()
}
}
}
fun getFileDirectory(context: Context): File {
return if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) {
context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)!!
} else {
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
}
}
Android Manifest
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path path="Android/data/${applicationId}/" name="files_root" />
<root-path
name="root"
path="/" />
</paths>