Download manager downloads file as (.bin) [kotlin] - android

I have my source file links on google drive. I have the file links on my project. now I need to force download the file when i click download button. I have used download manager it triggers download but it downloads as (.bin). I my case I cannot give the extension as (.png or .jpg) because the file can be any type it can be (image file, zip file, or 3D files like .obj, .glb) so how can i download as the extension of the original file.
private fun downloaad(zipFile: String, title: String) {
val downloadManager = activity?.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val source = Uri.parse(zipFile)
val request = DownloadManager.Request(source)
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION)
downloadManager.enqueue(request)
Toast.makeText(requireContext(), "Downloaded successfully", Toast.LENGTH_SHORT).show()
}
I tried this function it just downloads as .bin

Related

I want to store a .7z file into specific folder in phone internal storage using kotlin

I have found a code on stackoverflow,
val data: String = "hello 123"
val path = this.getExternalFilesDir(null)
val folder = File(path, "folder1")
folder.mkdirs()
println(folder.exists()) // u'll get true
val file = File(folder, "message.txt")
file.appendText(data)
by using this I was able to achieve this :-
// internal storage --> Android /data/com.example.testing123/files/folder 1/message.txt --> hello 123
but what I want is:-
// internal storage --> Android/data/inside a already available folder (check if folder is there or not, if not then create it) com.heaven.supermaxgame/store a .7z file
So please help me, I am new to kotlin, how can I store a 7zip file into a specific folder inside android --> data -->specific folder

How to get File object from assets?

I have a markdown file in my assets folder. I am using a markdown viewer which takes java.io.File Object as an argument to parse the file on screen.
But i don't know how to get a File Object from a file in assets folder.
Is there a way i can achieve this. I tried URI way but it failed.
This is my code snippet which may help
val markDown = File("file:///asset/GalacticAstronomy.md")
if (markDown.exists()) {
Toast.makeText(baseContext, "File exist", Toast.LENGTH_SHORT).show()
} else Toast
.makeText(baseContext, "File doesn't exist", Toast.LENGTH_SHORT).show()
setContent {
MaterialTheme {
MarkDown(
file = markDown,
modifier = Modifier.fillMaxSize()
)
}
}
I am not trying to read the file, So using assets.open("file") isn't going to work
But i don't know how to get a File Object from a file in assets folder
That is not possible, as it is not a file.
I am not trying to read the file, So using assets.open("file") isn't going to work
You do not have a choice. Either:
Use a better markdown library, one that accepts an InputStream, or
Copy the asset to a file yourself (e.g., in getCacheDir()) using assets.open()), then use the resulting file with your existing library

Download Manager notification not showing when downloading a file

I have an application where i have a feature to download a file already stored in firebase. i have implemented the code to download using download manager and at the same time show the user the notification of downloading that file but for some reason it is not working.
This is my code
val downloadRequest = DownloadManager.Request(Uri.parse(model.document.substringBeforeLast(".")))
downloadRequest.setTitle("الملف")
downloadRequest.setDescription("تحميل الملف")
downloadRequest.setVisibleInDownloadsUi(true)
downloadRequest.setDestinationUri(uri.toUri())
downloadRequest.allowScanningByMediaScanner()
downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
downloadManager.enqueue(downloadRequest)
Is the file getting downloaded but notification is not visible or file is not getting downloaded either?
try this and let me know if it still doesn't work :)
downloadRequest.setNotificationVisibility(
DownloadManager.Request.VISIBILITY_VISIBLE
)
EDIT
it seems that the last time I used it there was a difference in the destination that you are specifying.
val directory: File = File(Environment.getExternalStorageDirectory().path + "/myFile")
if(!directory.exists()) {
directory.mkdir()
}
...
downloadRequest.setDestinationInExternalPublicDir("/myFile","android")

How to copy a .txt file in data folder into the Download folder in Android using Kotlin?

I want to copy a .txt file that's in my app's package folder to the Download folder when a button is pressed. This is the code for the button:
binding_second.btnDone.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
var this_dir = getExternalFilesDir(this_filename)
var target_dir = File("sdcard/Download/"+ this_filename)
this_dir?.copyTo(target_dir)
println("Copy succeeded")
}
})
When this code is run, instead of copying the .txt file and its content from the source location and making a copy in the Download folder, it creates a folder with the name of the .txt file within the Download folder. I would appreciate any help at all!
You're confused about how getExternalFilesDir works. The parameter to it is NOT a filename within the directory. It's the type of external file directory, such as Environment.DIRECTORY_PICTURES for pictures. Since you passed an invalid value, you're getting the default directory. Then you're copying that directory.
What you want instead is var this_dir = File(getExternalFilesDir(null), this_filename); That will get a file with the given name relative to the getExternalFilesDir directory.

Prevent deleting files downloaded with DownloadManager on app uninstall

I've noticed that files downloaded using DownloadManager get deleted if my app gets uninstalled.
Is there a way to prevent this?
Update:
Here is kotlin code that I use to enqueue download:
val downloadId = DownloadManager.Request(Uri.parse(downloadUrl)).run {
setTitle(title)
setDescription(title)
setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
setAllowedOverMetered(true)
setAllowedOverRoaming(true)
setDestinationUri(file)
downloadManager.enqueue(this)
}
... where file is of the form: /storage/<sdcard-id>/Movies/<filename>.mp4

Categories

Resources