3

I have implemented http server on raspberry device which has it own network without access to internet. Now, I am developing Xamarin.Android application which needs to download files stored on server. When I connect to raspberry network there is no internet connection. When I try to download any file from server via http using Android DownloadManager instance nothing happens. There is no notification and no error at all. But when I change network in my phone to my local wifi I have internet access and then notification about downloading failure shows up.

It seems that DownloadManager do not work offline, but customer don't want internet connection on raspberry device.

I created my download manager instance based on:

Android: How to use download manager class?

Download Manager not working

Here is the code:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);

        LoadApplication(new App());

        if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) != Android.Content.PM.Permission.Granted || ActivityCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != Android.Content.PM.Permission.Granted)
        {

            // this will request for permission when user has not granted permission for the app 
            ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.ReadExternalStorage, Manifest.Permission.WriteExternalStorage }, 1);
        }

        else
        {
            //Download Script

            DownloadManager downloadManager = (DownloadManager)GetSystemService(Context.DownloadService);
            Android.Net.Uri uri = Android.Net.Uri.Parse("http://192.168.43.147:8080/data/22f13b77-d3cc-4788-96b3-cf454d4c07b6_IMG_20191008_122456.jpg");
            DownloadManager.Request request = new DownloadManager.Request(uri);
            request.SetAllowedNetworkTypes(DownloadNetwork.Wifi | DownloadNetwork.Mobile);
            request.SetVisibleInDownloadsUi(true); request.SetNotificationVisibility(Android.App.DownloadVisibility.Visible);
            request.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, uri.Path.Replace('\\', '/').Split('/').Last());
            downloadManager.Enqueue(request);
        }
    }
}

Server works and correctly serve files because I can download it via web browser on my laptop.

How can I achieve this without download manager, or how to force download manager to work offline?

Adam Mrozek
  • 1,410
  • 4
  • 26
  • 49
  • `request.SetAllowedNetworkTypes(DownloadNetwork.Wifi | DownloadNetwork.Mobile);` Why Mobile also? That's internet. – blackapps Oct 08 '19 at 14:54
  • This is my fifth approach. I tried without those options and result was the same. – Adam Mrozek Oct 08 '19 at 14:58
  • You can always download a file yourself with HttpUrlConnection. – blackapps Oct 08 '19 at 16:57
  • Sure, but I cannot save downloaded file in Downloads directory without using DownloadManager. Whenever I try to save there is no error and file is present in /data/apname/Downloads but it is not visible in download folder on android. Maybe I do something wrong but I thought that without DownloadManager will not able to do that – Adam Mrozek Oct 08 '19 at 18:22
  • If you use HttpUrlConnection you can store the file where you want if you use Storage Access Framework. Let the user choose the Download directory with ACTION_OPEN_DOCUMENT_TREE. Or let the user create or choose a file in the Download directory with ACTION_CRREATE_DOCUMENT or ACTION_OPEN_DOCUMENT. – blackapps Oct 08 '19 at 18:30
  • 1
    Besides that i have a hard time to believe that downloadmanager needs internet connection. – blackapps Oct 08 '19 at 18:31
  • `/data/apname/Downloads` you mean `/data/data/apname/Downloads`? – blackapps Oct 08 '19 at 18:32
  • https://stackoverflow.com/questions/58288397/downloadmanager-wont-load-a-file-from-localhost – blackapps Oct 08 '19 at 18:35
  • `http://192.168.43.147` Are you trying to download from an Android device acting as a hotspot? Id the Android device where your app is running connected to the same hotspot? – blackapps Oct 08 '19 at 18:49
  • `But when I change network in my phone to my local wifi I have internet access` You can let your local wifi on but detach the cable from the wall to the router. So no internet but wifi yes. Does it work? – blackapps Oct 08 '19 at 18:51

0 Answers0