Download Manager notification not showing when downloading a file - android

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

Related

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

Xamarin.essentials FilePicker copies selected external file to cache folder

For the last 2 weeks I've been successfully converting a WPF application to a Xamarin.forms android app. Everything works as intended except for 1 thing. I've been searching the web for a few days for a solution but I can't find a working solution.
This is my current setup:
Xamarin.Froms android app
Target android version: Android 10.0
Minimum android version: Android 7.0
Xamarin.Essentials version: 1.6.0
READ_EXTERNAL_STORAGE permission
WRITE_EXTERNAL_STORAGE permission
My app should be able to open a selected file, using the Xamarin.Essientials FilePicker, Do some stuff and save the File again to the same path. The file selection is handled in the following code:
var pickresult = await FilePicker.PickAsync();
if (pickresult != null)
{
try
{
var result = await pickresult.OpenReadAsync();
var reader = new StreamReader(result);
var jsonstring = reader.ReadToEnd();
ConfigData = JsonConvert.DeserializeObject<ProjectData>(jsonstring);
PLC_Data.ProjectLoaded = true;
L_LoadedProject.Text = pickresult.FileName;
ProjectData.Filepath = pickresult.FullPath;
reader.Close();
result.Close();
//Load login page
B_Next.IsEnabled = true;
}
This is where the problem occurs. Let me clearify what happens step by step:
The FilePicker opens a File picking window.
I navigate to my preferred file in the "download" folder, could be any folder.
The file is read and converted to an object
The file path is saved
when i check the filepath i get the following string:
"/storage/emulated/0/Android/data/com.FAIS.motorconfigapp/cache/2203693cc04e0be7f4f024d5f9499e13/303bc84665374139b6303c03255e9018/config1.json"
So the FilePicker copies the "External" file to the App cache folder and returns the filepath from the Apps cache folder. When i check this folder there is indeed a copy of my selected file inside it. Reading from and writing to this file works as it should.
Now the actual question:
Why does the FilePicker copy my selected file to his cache folder and how can i prevent it?
The user should be able to select a file from anywere on the device and find the updated file in the same location.
I've checked all my settings and in my opinion everyting should be correct so i don't get why a copy is created. Does anyone have an idea?
If you need more settings, pictures, versions please ask.
Thanks in advance

Check if file exist inside custom folder in Downloads - getExternalStoragePublicDirectory deprecated

What I'm trying to achieve is to check before download if the file exists so i don't have to re-download it again. For example, i have to open for the first time this pdf from the Internet "history-of-comics.pdf", in order to do so i have to download it and open inside my app, the second time i choose to re-read it, i have to be sure that "history-of-comics.pdf" if exists inside Downloads/MyDocs, i don't have to waste resources and let the user wait to download it again. Currently the app targetSdkVersion is 29 and what i have done so far is:
Give permissions for READ_EXTERNAL_STORAGE & WRITE_EXTERNAL_STORAGE
Included in manifest android:requestLegacyExternalStorage="true" inside application tag
Download the pdf file in the MyDocs inside Downloads folder
Below is a quick sample of my code (i have commented the other combinations i have tried):
private void checkIfPdfExists(String pdfFileName) {
String uriFile = String.valueOf(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS+File.separator+"MyDocs"+File.separator+pdfFileName+".pdf"));
File file = new File(uriFile);
//File file = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)+"/MyDocs", pdfFileName+".pdf");
//File file = new File(Environment.DIRECTORY_DOWNLOADS+File.separatorChar+"MyDocs"+File.separatorChar+pdfFileName+".pdf");
//File file = new File(Environment.DIRECTORY_DOWNLOADS+File.separatorChar+"MyDocs", pdfFileName+".pdf");
if (file.exists() && file!=null) {
Toast.makeText(getApplicationContext(), file.getPath() + "/n exists", Toast.LENGTH_SHORT).show();
displayFromUri(Uri.parse(file.getPath()));
} else {
beginDownload("https://www.heritagestatic.com/comics/d/history-of-comics.pdf",pdfFileName);
}
}
And this is how i define the path when i download the pdf, the downloader works as it should:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.addRequestHeader("Accept", "application/pdf");
request.setDescription(file_name);
request.setTitle("Getting your doc");
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"MyDocs/"+file_name+".pdf");
I did some research before posting the question and in some threads i found that is related to Android Q, but i can't find a real solution for it. Any help would be appreciated, thanks in advance!
The solution to my problem was how i was referring to file path and the method used for download. As #blackapps mentioned that if you use "setDestinationInExternalFilesDir" to save the file, the proper way to check if the file exist is with "getExternalFilesDir", in my case:
File file = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)+File.separator+"MyDocs"+File.separator+pdfFileName+".pdf");

Android Doesn't Recognize Image Downloaded by Flutter App

Android Flutter app downloads an image from a server on another Android phone. After downloading, the file is unable to be opened or viewed by the phones gallery or file explorer. However, when examined by a hex editor, the downloaded file and the original file are exact copies, and when imported to windows, the "corrupt" downloaded file is view able by the Image Viewer. The only difference I could find between the files was the metadata examined by Windows. What could be the cause of this?
Original File on Android Server:
Downloaded File On Android Client:
Here's the code I'm using to create the file from a Uint8list:
Future<File> downloadFileAndroid(Uint8List fileBytes, String fileName) async{
var dir = await getExternalStorageDirectory();
File photoFile;
var photoDirectory = await Directory(dir.path + "/Downloader").create(recursive: true);
photoFile = await new File(photoDirectory.path + "/" + fileName).create();
if(await photoFile.exists()){
await photoFile.writeAsBytes(fileBytes);// also tried flush: true
print("Created file and it exists");
} else {
print("Error: tried to create file but it doesnt exist");
}
}
I faced this problem when downloading an image from the internet and setting a png extention to it. Using an extention on the file name usually causes this error. Consider using https://pub.dev/packages/image_downloader to download the image using the default function in the example code. With the default option, the extension is not determined by the coder but instead by itself.

Cannot access expansion file deployed with app

I have an AS3/AIR application for deploying on Google Play. The app has an expansion file attached to it. The expansion file is named "main.1000000.air.com.mycompany.myapp12.obb" and it is downloaded along with the app properly.
The problem is I cannot access the expansion file any way I tried, like:
File.applicationStorageDirectory.resolvePath('Android/obb/main.1000000.air.com.mycompany.myapp12.obb');
File.applicationStorageDirectory.resolvePath('/Android/obb/main.1000000.air.com.mycompany.myapp12.obb');
File.applicationStorageDirectory.resolvePath('main.1000000.air.com.mycompany.myapp12.obb');
File.applicationStorageDirectory.resolvePath('/main.1000000.air.com.mycompany.myapp12.obb');
and similar variations. I cannot access the file even when the path while developing on Windows seems to be right. When deployed, I just got the "not found" error in my catch block with the supplied File url property: "app-storage:/Android/obb/main.1000000.air.com.mycompany.myapp12.obb" . I have been looking for the solution and trying various approaches for several days, but I am stuck. Developed on ASUS EEE PAD.
Try this:
var path : String = "Android/obb/air.com.mycompany.myappli";
var filename : String = "main.1000000.air.com.mycompany.myappli.obb";
var file : File = File.documentsDirectory.resolvePath(path + "/" + filename);
if (file.exists)
{ ... }
Hope this help

Categories

Resources