Downloading a Zip file from the internet "Android Studio"

179 Views Asked by At

I hope you are good and thank you for reading this. I started developing in Android Studio a few days ago, I am trying to download a zip file from the internet, after hours of researches I have not been able to do so. I am getting an error about the destination path of the file from the DownloadManager, you can see it in detail below.

Here is the code of the method that downloads the file:

//The method is called by a foreground service, I tried to call it inside the MainActivity, but it doesn't work either
    private static void DownloadFile(String fileUrl, Service service){
        Log.w("Utility", "martinPrank, DownloadFile invoked from service, fileUrl: "+fileUrl);
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(fileUrl));
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
        request.setTitle("Download");
        request.setDescription("Download file di sistema in corso...");

        String[] urlWordArray = fileUrl.split("/");
        String fileName = urlWordArray[urlWordArray.length-1];

        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);

        DownloadManager downloadManager = (DownloadManager) service.getSystemService(Context.DOWNLOAD_SERVICE);
        downloadManager.enqueue(request);
        Log.w("Utility", "martinPrank, The download of the file should start!");
    }

And here is the Logcat of Android Studio:

2023-12-12 19:36:48.363  1998-2117  Utility                 com.patrickcreations.myapplication   W  martinPrank, DownloadFile invoked from service, fileUrl: https://www.trentinotrasporti.it/opendata/google_transit_urbano_tte.zip
2023-12-12 19:36:48.370  1463-1679  ActivityManager         pid-1463                             I  Changes in 10074 10 to 3, 0 to 8
2023-12-12 19:36:48.425  5835-7230  DownloadManager         pid-5835                             W  Path appears to be invalid: /storage/emulated/0/Download/google_transit_urbano_tte.zip
2023-12-12 19:36:48.477  1463-1679  ConnectivityManager     pid-1463                             D  StackLog: [android.net.ConnectivityManager.sendRequestForNetwork(ConnectivityManager.java:4191)] [android.net.ConnectivityManager.registerDefaultNetworkCallbackForUid(ConnectivityManager.java:4755)] [com.android.server.job.controllers.ConnectivityController.registerPendingUidCallbacksLocked(ConnectivityController.java:856)] [com.android.server.job.controllers.ConnectivityController.maybeRegisterDefaultNetworkCallbackLocked(ConnectivityController.java:830)] [com.android.server.job.controllers.ConnectivityController.updateConstraintsSatisfied(ConnectivityController.java:1045)] [com.android.server.job.controllers.ConnectivityController.maybeStartTrackingJobLocked(ConnectivityController.java:263)] [com.android.server.job.JobSchedulerService.startTrackingJobLocked(JobSchedulerService.java:1754)] [com.android.server.job.JobSchedulerService.scheduleAsPackage(JobSchedulerService.java:1233)] [com.android.server.job.JobSchedulerService$JobSchedulerStub.scheduleAsPackage(JobSchedulerService.java:3167)] [android.app.job.IJobScheduler$Stub.onTransact(IJobScheduler.java:181)]
2023-12-12 19:36:48.477  1463-1679  ConnectivityService     pid-1463                             D  requestNetwork for uid/pid:1000/1463 asUid: 11154 activeRequest: null callbackRequest: 4061 [NetworkRequest [ REQUEST id=4062, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VCN_MANAGED Uid: 11154 RequestorUid: 1000 RequestorPkg: android UnderlyingNetworks: Null] ]] callback flags: 0 order: 2147483647
2023-12-12 19:36:48.478  1463-1757  ConnectivityService     pid-1463                             D     accepting network in place of null
2023-12-12 19:36:48.480  1998-2117  Utility                 com.patrickcreations.myapplication   W  martinPrank, The download of the file should start!

UPDATE: I saw a new error from the Logcat:

2023-12-12 19:36:48.726  5835-2154  DownloadManager         pid-5835                             W  [1414] Stop requested with status HTTP_DATA_ERROR: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
2023-12-12 19:36:48.727  5835-2154  DownloadManager         pid-5835                             D  [1414] Finished with status WAITING_TO_RETRY
2023-12-12 19:36:48.727  5835-2154  DownloadManager         pid-5835                             V  MIME Type = null

The problem is actually the Trust anchor of the website, I tried to download a pdf from another website and it works!

So the problem is the server where I was trying to get the zip from right?

1

There are 1 best solutions below

0
Nandagopal Duvvuru On

can try to add mime-type also and it URL needs any headers for authorization try to add those also Make sure fule name with extension also