Prevent deleting files downloaded with DownloadManager on app uninstall - android

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

Related

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")

Android Download Manager giving 400 'placeholder' error in androidX kotlin

I am trying to download some file using android download manager, it always gives 400 error, and download unsuccessful message.
fun DownloadData(uri: Uri): Long {
val downloadReference: Long
downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val request = DownloadManager.Request(uri)
//Setting title of request
request.setTitle("Test Download")
//Setting description of request
request.setDescription("Android Data download using DownloadManager.")
//Set the local destination for the downloaded file to a path within the application's external files directory
request.setDestinationInExternalFilesDir(this#MainActivity, Environment.DIRECTORY_DOWNLOADS, "AndroidTutorialPoint.mp3")
//Enqueue download and save the referenceId
downloadReference = downloadManager!!.enqueue(request)
return downloadReference
}
I am calling above function by
val uri = Uri.parse("https://file-examples.com/wp-content/uploads/2017/04/file_example_MP4_1920_18MG.mp4")
downloadId = DownloadData(uri)
unable to understand where am i going wrong? the same code works in API level 26, it stopped working after migrating to androidx, and api 29
EDIT: it doesnt work on API level 28 and later irrespective of androidX, shows download unsuccessful

How to fix DownloadManager can't open file and file missing?

I have project using downloadManager, i already can show the download progress in screen, but the problem is when after i download the file then click the download complete notif it said that "Can't open file"
This is my code,
public class MainActivity extends AppCompatActivity {
Button tombolDownload;
DownloadManager downloadManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tombolDownload = (Button) findViewById(R.id.btnDownload);
tombolDownload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
downloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse("http://192.168.1.64/FileAPK/app1.apk");
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Long reference = downloadManager.enqueue(request);
}
});
}
}
How i can fix this problem?
I think you should declare the MIME type also of APK file.
MIME type is : application/vnd.android.package-archive .
Use this as following:
request.setMimeType("application/vnd.android.package-archive");
Edit: Add the following line also so that it knows where to store it. In your code it was saving the file with some random number without extension, hence the can't open file problem was happening.
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"app1.apk");
Where app1.apk is name of file with extension and also add setMimeType()
dont forget the persmissions WRITE_EXTERNAL_STORAGE
hope it helps
I had a similar issue with the download of a zip file.
Upon attempting to open the downloaded file by clicking on the Download Manager's notification, I would get the same Can't open file notification.
In my particular case, I learned that it had nothing to do with the code itself – as the file was indeed being properly downloaded – but with the fact that the device being used did not have an installed app which could handle zip files.
After I installed a 3rd-party app which was enabled to handle zip files, I wouldn't have this issue anymore, as the the downloaded file would now be opened by that app.

Android DownloadManager not deleting the physical file when remove is called

I am noticing some strange behaviour while using DownloadManager class to download videos from my server.
The videos are downloaded just fine, but when I try to delete them using the public int remove (long... ids) function, the physical file doesn't get deleted and memory ultimately isn't released either.
Here's how i am giving the DownloadManager.Request object to the DownloadManager's enqueue function:
Uri target = Uri.fromFile(new File(destinationFolder, Sha1Util.SHA1(url)));
Request request = new Request(Uri.parse(url));
request.setDestinationUri(target);
request.setNotificationVisibility(Request.VISIBILITY_HIDDEN);
request.setVisibleInDownloadsUi(false);
where destinationFolder = /storage/emulated/0/Android/data/{app's_package_name}/{user_name}
NOTE: The remove function is working fine on Lollipop devices but isn't working on prior versions.

Android Download Manager won't stop

I'm not exactly sure what my problem is. I using the code below to download a bunch of files:
public void DownloadNow(ArrayList<String> must_download)
{
String serviceString = Context.DOWNLOAD_SERVICE;
DownloadManager download;
download = (DownloadManager)getSystemService(serviceString);
for(int x = 0; x < must_download.size(); x++)
{
System.out.println("DOWNLOADING "+must_download.get(x).toString());
Uri uri = Uri.parse("http://drm.csdev.nmmu.ac.za/Zandre/"+must_download.get(x).toString());
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, must_download.get(x).toString());
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
request.setTitle(must_download.get(x).toString());
long reference = download.enqueue(request);
}
}
Where must_download is an ArrayList full of file names. This method has successfully worked in the past, until I tried to run it on a different thread. The thread idea did not work out so well, so I decided to switch back.
Now, the problem is this- since I switched back to the original method the Download Manager runs full time, downloading the same files over and over in an endless loop.The only way I can stop it is to **Disable the Download Manager, but then I can't download anything...
What I've read so far and tried is
Uninstall the app- still downloading...
Switch device off and back on again- still downloading...
Clear cache of Download Manager- still downloading...
Even after this has used all the devices mobile data, it still just carries on downloading.
Has anyone else encountered this type of problem? Any ideas solutions?

Categories

Resources