I have audio 'url' "https://my-server.com/555.mp3"
how can I download it to "externalCacheDir" to be easy access without internet
i tried to use DownloadManager but i think there is better way to do it
fun downloadAudio(url:String){
val request = DownloadManager.Request(url.toUri())
val externalCachPath = getApplication<Application>().externalCacheDir!!.absolutePath
val fileName = "${UUID.randomUUID()}.3gp"
request.setDestinationInExternalPublicDir(externalCachPath,fileName)
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
request.setDescription("Download")
request.setTitle(fileName)
download(request)
return filename
}
Related
I am trying to download a file into the external public directory but the Uri returned by downloadManager.getUriForDownloadedFile(requestId) isn't usable. I'm unable to launch an ACTION_OPEN intent with it, even though this same process works for Android 10.
I suspect this has something to do with missing updated permissions on Android 13, but there are no errors logged in logcat.
I am able to get it working as expected by using setDestinationInExternalFilesDir to store the file inside the private applications directory and using a ContentResolver to copy it into the phones external media storage, but that is a lot of code and very verbose. Using setDestinationInExternalPublicDir from DownloadManager is a lot cleaner and concise.
This is how I am creating and enqueuing my request
val request = DownloadManager
.Request(Uri.parse(downloadUrl))
.setDestinationInExternalPublicDir(
Environment.DIRECTORY_MOVIES,
"${UUID.randomUUID()}.mp4"
)
.setMimeType("video/mp4")
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setTitle("Saving...")
.setRequiresCharging(false)
.setAllowedOverMetered(true)
.setAllowedOverRoaming(true)
requestId = downloadManager.enqueue(request)
And this is how I am listening for download completion and attempting to use the Uri.
private val downloadBroadCastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val requestId = intent?.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1) ?: -1
val query = DownloadManager.Query()
query.setFilterById(requestId)
val cursor = downloadManager.query(query)
if (cursor.moveToFirst()) {
val columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)
when (cursor.getInt(columnIndex)) {
DownloadManager.STATUS_SUCCESSFUL -> {
LOG.info("onReceive: Video download completed!")
val uri = downloadManager.getUriForDownloadedFile(requestId)
context.startActivity(
Intent(Intent.ACTION_VIEW, uri).apply {
setDataAndType(uri, "video/mp4")
}
)
}
}
}
}
I am using Download Manager to Download PDFs and Images. Downloaded files are not getting saved in Download Folder. Here is my code.
`
val fileName = "MG Uploaded Documents"
val request = DownloadManager.Request(Uri.parse(downloadUrl))
.setTitle("Download")
.setDescription("Downloading")
request.setDestinationUri(
Uri.fromFile(File(this.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
.toString() , fileName)))
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setAllowedOverMetered(true)
val dm = this.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
dm.enqueue(request)
this.T("File is Downloading. Please wait!")
`
This code is in Home Activity. Any help.
I was not saving the file name with .pdf extension.
Below is the working code.
private fun downloadPDF(url: String) {
val fileName = "YourPdfName.pdf"
val request = DownloadManager.Request(Uri.parse(url))
.setTitle("PDF Download")
.setDescription("Downloading")
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setAllowedOverMetered(true)
// val dm = getSystemService(DOWNLOAD_SERVICE) as DownloadManager
val dm = requireContext().getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
dm.enqueue(request)
}
I am trying to create an android app with kotlin, this app need to have a mini download manager as I will need to download files from 100MB to 8GB and user can pause and resume download later when the server supports the pause, searching I found the Ktor library and reading the documentation plus some videos on youtube, I managed to write a base code where I could download the files and make the process of stopping the download and keep going all right when one of mine tests gave error there are files whose url pattern is: http://server.com/files?file=/10/55/file.zip
The problem is that I put this link, but Ktor converts to http://server.com/files?file=%2F10%2F55%2Ffile.zip this generate an error response on the server, as I don't have access to the server to change this rule I need to send the right url without encoding. Does anyone know how to do this? Prevent Ktor from doing a URL_encode in the url parameters, I couldn't find anything in the documentation
My code is this:
ktor-client version 1.6.7
fun startDownload(url: String, auth: String = "", userAgentS: String = "", fileName: String = ""){
val client = HttpClient(CIO)
val path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
val file = File.createTempFile("File", "index", path)
runBlocking {
client.get<HttpStatement>(url){
headers {
append(HttpHeaders.Authorization, auth)
append(HttpHeaders.UserAgent, userAgentS)
append(HttpHeaders.Range, "bytes=${file.length()}-")
}
}
.execute { httpResponse ->
val channel: ByteReadChannel = httpResponse.receive()
while (!channel.isClosedForRead) {
val packet = channel.readRemaining(DEFAULT_BUFFER_SIZE.toLong())
while (!packet.isEmpty) {
val bytes = packet.readBytes()
file.appendBytes(bytes)
println("Received ${(file.length())} bytes from ${httpResponse.contentLength()}")
}
}
val pathF = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + "/${fileName}")
file.renameTo(pathF)
println("A file saved to ${file.path}")
}
}
}
Can anyone help me solve this problem with ktor, if there is no solution, can someone tell me another way to achieve the same goal? Need to be with Kotlin.
update 2022-02-17
Thanks to Aleksei Tirman's help I managed to solve the problem, thank you very much. And the base code looks like this:
fun startDownload(url: String, auth: String = "", userAgentS: String = "", fileName: String = ""){
val client = HttpClient(CIO)
val path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
val file = File.createTempFile("File", "index", path)
runBlocking {
client.get<HttpStatement>(url){
url {
parameters.urlEncodingOption = UrlEncodingOption.NO_ENCODING
}
headers {
append(HttpHeaders.Authorization, auth)
append(HttpHeaders.UserAgent, userAgentS)
append(HttpHeaders.Range, "bytes=${file.length()}-")
}
}
.execute { httpResponse ->
val channel: ByteReadChannel = httpResponse.receive()
while (!channel.isClosedForRead) {
val packet = channel.readRemaining(DEFAULT_BUFFER_SIZE.toLong())
while (!packet.isEmpty) {
val bytes = packet.readBytes()
file.appendBytes(bytes)
println("Received ${(file.length())} bytes from ${httpResponse.contentLength()}")
}
}
val pathF = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + "/${fileName}")
file.renameTo(pathF)
println("A file saved to ${file.path}")
}
}
}
You can disable query parameters encoding by assigning the UrlEncodingOption.NO_ENCODING value to the urlEncodingOption property of the ParametersBuilder. Here is an example:
val requestBuilder = HttpRequestBuilder()
requestBuilder.url {
protocol = URLProtocol.HTTP
host = "httpbin.org"
path("get")
parameters.urlEncodingOption = UrlEncodingOption.NO_ENCODING
parameters.append("file", "/10/55/file.zip")
}
val response = client.get<String>(requestBuilder)
I am trying to download a pdf file from the server (I have the url), and I am trying this solution
fun downloadPDF(url: String?, fileName: String): ResponseStatus {
val uri = Uri.parse(url)
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager?
val request = DownloadManager.Request(uri)
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE
)
request.setTitle(fileName)
request.setDescription("Android Data download using DownloadManager.")
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)
request.setMimeType("*/*")
if (downloadManager == null) {
return ResponseStatus.Error( context.getString(R.string.download_error) )
}
downloadManager.enqueue(request)
return ResponseStatus.OK(uri)
}
this works for API 30 but not for API 27 and I don't know why.
Can someone help me please?
UPDATE:
I think that this can be a permissions problem because I tried again in API 27 and see this in the log java.lang.SecurityException: No permission to write to /storage/emulated/0/Download/file name12-34: Neither user 10080 nor current process has android.permission.WRITE_EXTERNAL_STORAGE.
I was able to solve the problem, I changed this line
request.setDestinationInExternalPublicDir (Environment.DIRECTORY_DOWNLOADS, fileName) for a uri that I parse with the address of the download folder and the file name, and I set it with request.setDestinationUri (destinationUri)
val destination = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() +
"/" + fileName
val destinationUri = Uri.parse("$FILE_BASE_PATH$destination")
I have written a c# web service that returns a pdf in a stream of bytes as response. Once I make a call to the web-service from my android app, I will store the response in an array byte till here I will be able to do it. But after that I need to convert that byte array into pdf, I should be able to display that. I have a menu page in which once the button is pressed the call is made to the web service with file name and on click of button I should be able to open pdf. Is this possible? Or there is some other, better solution? I checked on the net for better understanding, but I was unable to find one that could help me understand better.
Thanks for the suggestion, but I don't have the pdf in hand, I just have the array bytes, which I got from the web service. So I now need to regenerate the pdf from this array of bytes and display it, but I am not getting how to do it.
Try following these steps
Convert byte array to InputStream
val inputStream = ByteArrayInputStream(byteArray)
Save InputStream as PDF file:
suspend fun saveInputStreamAsPdfFile(inputStream: InputStream, applicationContext: Context): File? {
var outputFile: File? = null
withContext(Dispatchers.IO) {
try {
val directory = ContextCompat.getExternalFilesDirs(applicationContext, "documents").first()
val outputDir = File(directory, "outputPath")
outputFile = File(outputDir, UUID.randomUUID().toString() + ".pdf")
if (!outputDir.exists()) {
outputDir.mkdirs()
}
val outputStream = FileOutputStream(outputFile, false)
inputStream.use { fileOut -> fileOut.copyTo(outputStream) }
outputStream.close()
} catch (e: Exception) {
// Something went wrong
}
}
return outputFile
}
Show PDF with PdfRenderer
var totalPdfPages: Int
fun showPdf(pdfFile: File) {
val input = ParcelFileDescriptor.open(pdfFile, MODE_READ_ONLY)
val renderer = PdfRenderer(input)
val wrapper = PdfRendererWrapper(renderer)
totalPdfPages = wrapper.getTotalPages()
showPdfPage(0)
}
fun showPdfPage(currentPageIndex: Int) {
val pageBitmap = wrapper.getBitmap(currentPageIndex)
imageView.setImageBitmap(pageBitmap) // Show current page
}