Recent

Author Topic: How to download a file from web page displayed by jWebView  (Read 6523 times)

maurobio

  • Hero Member
  • *****
  • Posts: 611
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #60 on: June 08, 2023, 05:58:36 pm »
BobDog,

We are dealing with ANDROID development, not "Windows".

Thank you.
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

maurobio

  • Hero Member
  • *****
  • Posts: 611
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #61 on: June 08, 2023, 07:53:03 pm »
Hi, @BobDog!

It is OK, sir! No need for apologies - I understand  you are willing to help!

This vexing problem is rising the blood pressure of us all...

Enjoy your tea (I love tea too).

Best regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

c4p

  • Full Member
  • ***
  • Posts: 133
Re: How to download a file from web page displayed by jWebView
« Reply #62 on: June 08, 2023, 10:48:15 pm »
Hey @maurobio

This is my attempt, I have tweaked to so it 'should' just be a copy/paste into your unit1.pas file, apologies if I screwed anything up, the code should work, if you copy/paste to the current events, it works perfect for me, click file, downloads, and opens when complete with whatever mime type you assign, this uses your project folder 'Download' directory, have a look, make a backup first!....fingers crossed  ;)
Just standard permissions required for WRITE_EXTERNAL_STORAGE required which is already in your manifest file.

Code: Pascal  [Select][+][-]
  1. {hint: Pascal files location: ...\ReptileDatabase\jni }
  2. unit unit1;
  3.  
  4. {$mode delphi}
  5.  
  6. interface
  7.  
  8. uses
  9.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  10.   cthreads,
  11.   {$ENDIF}{$ENDIF}
  12.   Classes, SysUtils, StrUtils, AndroidWidget, menu, Laz_And_Controls, And_jni,
  13.   customdialog, broadcastreceiver, downloadmanager, intentmanager, downloadservice;
  14.  
  15. type
  16.  
  17.   { TAndroidModule1 }
  18.  
  19.   TAndroidModule1 = class(jForm)
  20.     jBroadcastReceiver1: jBroadcastReceiver;
  21.     jDownloadService1: jDownloadService;
  22.     jIntentManager1: jIntentManager;
  23.     jWebView1: jWebView;
  24.     jMenu1: jMenu;
  25.     jPanel1: jPanel;
  26.     jPanel2: jPanel;
  27.     jPanel3: jPanel;
  28.     jTextView2: jTextView;
  29.     jTextView3: jTextView;
  30.     jWebView1: jWebView;
  31.     Panel1: jPanel;
  32.     Panel2: jPanel;
  33.     TextView1: jTextView;
  34.     TextView2: jTextView;
  35.     TextView3: jTextView;
  36.     TextView4: jTextView;
  37.     TextView5: jTextView;
  38.     TextView6: jTextView;
  39.     TextView7: jTextView;
  40.     procedure AndroidModule1ClickOptionMenuItem(Sender: TObject;
  41.       jObjMenuItem: jObject; itemID: integer; itemCaption: string;
  42.       Checked: boolean);
  43.     procedure AndroidModule1CreateOptionMenu(Sender: TObject; jObjMenu: jObject);
  44.     procedure AndroidModule1Show(Sender: TObject);
  45.     procedure AndroidModule1SpecialKeyDown(Sender: TObject; keyChar: char;
  46.       keyCode: integer; keyCodeString: string; var mute: boolean);
  47.     procedure jBroadcastReceiver1Receiver(Sender: TObject; intent: jObject);
  48.     procedure jWebView1Status(Sender: TObject; Status: TWebViewStatus;
  49.       URL: String; var CanNavi: Boolean);
  50.   private
  51.     {private declarations}
  52.   public
  53.     {public declarations}
  54.   end;
  55.  
  56. var
  57.   AndroidModule1: TAndroidModule1;
  58.   DownloadURL, URLFileName, Extension: string;
  59.   res: TAndroidResult;
  60.  
  61. implementation
  62.  
  63. {$R *.lfm}
  64.  
  65.  
  66. { TAndroidModule1 }
  67.  
  68. procedure TAndroidModule1.jWebView1Status(Sender: TObject;
  69.   Status: TWebViewStatus; URL: String; var CanNavi: Boolean);
  70. var
  71.   SlashPos: integer;
  72. begin
  73.   //Pass the URL variable to our own variable to use in jDownloadReceiver later
  74.   DownloadURL := URL;
  75.   //Extract the file extension from the URL
  76.   Extension := ExtractFileExt(URL);
  77.   if (Extension = '.xlsx') or (Extension = '.xls') or (Extension = '.pdf') then
  78.    begin
  79.     //Get filename after the last slash in the URL using SlashPos position
  80.     SlashPos := LastDelimiter('/', URL); //Get the last occurrence of slash in the URL
  81.     //Get the actual filename so can use this later to save the file with the same name
  82.     URLFilename := AnsiRightStr(URL, Length(URL) - Slashpos);
  83.  
  84.     //Let's delete the file if it exists in case we cache an old file, as we will need to download the latest version of the file
  85.     if fileexists(Self.GetEnvironmentDirectoryPath(dirDownloads)+'/'+URLFilename) then
  86.      begin
  87.        Self.DeleteFile(self.GetEnvironmentDirectoryPath(dirDownloads)+'/'+URLFilename);
  88.      end;
  89.  
  90.       //Register Intent for download
  91.       jBroadcastReceiver1.RegisterIntentActionFilter('org.lamw.AppShowDown.DOWNLOAD_RECEIVER'); //Register intent for Broadcast receiver
  92.       //What your downloaded file will be called, let's use the URL filename (there may be a method to get the filename?)
  93.       //Save file to project Download directory
  94.       jDownloadService1.SaveToFile(Self.GetEnvironmentDirectoryPath(dirDownloads), URLFIlename);
  95.       //Start the download
  96.       jDownloadService1.Start(URL, 'org.lamw.AppShowDown.DOWNLOAD_RECEIVER');
  97.       //Let user know we have started downloading
  98.       ShowMessage('Downloading file...please wait');
  99.    end;
  100. end;
  101.  
  102. procedure TAndroidModule1.jBroadcastReceiver1Receiver(Sender: TObject;
  103.   intent: jObject);
  104. begin
  105.          //Store result code so we can check later if the download was successful
  106.          res := jBroadcastReceiver1.GetResultCode();
  107.          //Use res to check for successful download or fail
  108.          if (res = RESULT_OK) then
  109.           begin
  110.            ShowMessage('Download of' + sLineBreak + DownloadURL + sLineBreak + 'successful');
  111.            //Launch the file once it has downloaded
  112.             if (Extension = '.xls') then        // alternative office document mime type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  113.              begin
  114.               Self.StartDefaultActivityForFile(Self.GetEnvironmentDirectoryPath(dirDownloads) + '/'+ URLFIlename,'application/vnd.ms-excel'); //Start the downloaded file using Excel
  115.              end
  116.             else if (Extension = '.xlsx') then
  117.              begin
  118.               Self.StartDefaultActivityForFile(Self.GetEnvironmentDirectoryPath(dirDownloads) + '/'+ URLFIlename,'application/vnd.ms-excel'); //Start the downloaded file using Excel
  119.              end
  120.             else if (Extension = '.pdf') then
  121.               begin
  122.                Self.StartDefaultActivityForFile(Self.GetEnvironmentDirectoryPath(dirDownloads) + '/'+ URLFIlename,'application/pdf'); //Start the downloaded file using PDF VIewer
  123.               end;
  124.            end
  125.              else if (res <> RESULT_OK) then
  126.               begin
  127.                ShowMessage('Download of' + sLineBreak + DownloadURL + sLineBreak + 'FAILED!');
  128.               end;
  129.  
  130.    //unregister BroadcastReceiver ...
  131.    jBroadcastReceiver1.Unregister();
  132. end;
  133.  
  134. procedure TAndroidModule1.AndroidModule1Show(Sender: TObject);
  135. begin
  136.     jWebView1.Navigate('http://biotupe.org/test/');
  137. end;
  138.  
  139. procedure TAndroidModule1.AndroidModule1SpecialKeyDown(Sender: TObject;
  140.   keyChar: char; keyCode: integer; keyCodeString: string; var mute: boolean);
  141. begin
  142.     if (KeycodeString = 'KEYCODE_BACK') then
  143.   begin
  144.     if jWebView1.CanGoBack then
  145.       jWebView1.GoBack;
  146.     mute := True;
  147.   end;
  148. end;
  149.  
  150. procedure TAndroidModule1.AndroidModule1CreateOptionMenu(Sender: TObject;
  151.   jObjMenu: jObject);
  152. var
  153.   i: integer;
  154. begin
  155.   for i := 0 to jMenu1.Options.Count - 1 do
  156.     jMenu1.AddItem(jObjMenu, i, jMenu1.Options.Strings[i], '',
  157.       mitDefault, misNever);
  158. end;
  159.  
  160. procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);
  161. begin
  162.   jWebView1.SetLoadWithOverviewMode(True);
  163.   jWebView1.SetUseWideViewPort(True);
  164. end;
  165.  
  166. procedure TAndroidModule1.AndroidModule1ClickOptionMenuItem(Sender: TObject;
  167.   jObjMenuItem: jObject; itemID: integer; itemCaption: string; Checked: boolean);
  168. begin
  169.   case itemID of
  170.     0:
  171.     begin
  172.       ShowMessage('The ShowDown App' + sLineBreak +
  173.         'This app demonstrates how to download and open' + sLineBreak +
  174.         'a linked file from a webpage with' + sLineBreak +
  175.         'the associated app' + sLineBreak + sLineBreak +
  176.         'This code does not belongs to anyone. Property is theft. Help yourself.',
  177.         gvCenter, slLong);
  178.     end;
  179.     1:
  180.       AndroidModule1.Close;
  181.   end;
  182. end;
  183.  
  184. end.
« Last Edit: June 08, 2023, 11:04:20 pm by c4p »
Lazarus 2.0.12 r64642/FPC 3.2.0 on Windows 10 22H2, projects mainly built using AppCompat and Gradle v6.6.1.

maurobio

  • Hero Member
  • *****
  • Posts: 611
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #63 on: June 08, 2023, 11:16:21 pm »
Hi, @c4p!

I got your message when I just finish another kind of test which presented some progress after more than a week of attempts: I got my app partially working on an old, cheap Hyundai "Maestro" tablet running Android 5.1.1! In this test, the app downloaded the file (this has never been a problem) and then correctly called the associated app (SmartOffice for xlsx, EBookDroid for pdf) but then each app complained that the file could not be opened. This was small progress, but at least I got evidence that the problem is really related to the version of Android running in the device.

As an aside: I also tried my app in a most modern (and not so cheap) Samsung Tab 8.0 running Android 11.00. But this time I got an error from LAMW itself - it complained that "could not install apk" and so the app has not even been installed into the device.

I will try your code with my personal device, which runs Android 10.0 and will report the results ASAP.

Thank you very much!

With warmest regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

maurobio

  • Hero Member
  • *****
  • Posts: 611
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #64 on: June 08, 2023, 11:37:02 pm »
Hi, @c4p!

When attempting to compile and install the modified app into the device, I got the following errors from LAMW:

Quote
Building APK (Ant)... : Sucess
Installing APK (Ant)... : Sucess
Starting APK... : Sucess
Starting: Intent { cmp=org.lamw.showdown/.App }
Error type 3
Error: Activity class {org.lamw.showdown/org.lamw.showdown.App} does not exist.

Of course, the app is not installed into the device.

Any hints?

Best regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

maurobio

  • Hero Member
  • *****
  • Posts: 611
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #65 on: June 08, 2023, 11:56:24 pm »
Hi, @c4p!

Don't worry about my previous message - there seem to be an inconsistency in the name speces of the AppShowDown.

I added your code to my own app and this time it has been compiled and installed without any LAMW complaints.

The app is installed and runs... the xlsx and pdf files are correctly downloaded... but then, as always, nothing happens! No associated app is called, no file is displayed.

Maybe my (very simple) device running Android 10 simply cannot run this sort of app. I am at a loss and about to give up.

Thanks a lot for your great efforts!

With warmest regards,

Best
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

c4p

  • Full Member
  • ***
  • Posts: 133
Re: How to download a file from web page displayed by jWebView
« Reply #66 on: June 09, 2023, 12:07:42 am »
Hmmm check to see if the files exist in the path: Android/data/org.lamw.appname/files/Download if you have not already done this, if you find the files, try opening them directly on the device.
See if that path exists on your device, you will need to have the device connected and in file transfer mode to get access if you cannot navigate to them on the device itself with a file manager.
It could be that StartDefaultActivityForFile is not available on Android 10, I am using Android 12!
I suspect we need another way to open the files directly using an intent, I think this may be on this thread somewhere, I will have a look.
Lazarus 2.0.12 r64642/FPC 3.2.0 on Windows 10 22H2, projects mainly built using AppCompat and Gradle v6.6.1.

maurobio

  • Hero Member
  • *****
  • Posts: 611
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #67 on: June 09, 2023, 12:25:09 am »
Hi, @c4p!

Quote
Hmmm check to see if the files exist in the path: Android/data/org.lamw.appname/files/Download if you have not already done this, if you find the files, try opening them directly on the device.

No such path exist in my system.

Quote
See if that path exists on your device, you will need to have the device connected and in file transfer mode to get access if you cannot navigate to them on the device itself with a file manager.

I did not know this (that to see certain folders the device need to be connected). Anyway, even if connected, the path "Android/data/org.lamw.appname/files/Download " does not exist. The files downloaded by the app are put in the Downloads folder of the main storage.

Quote
It could be that StartDefaultActivityForFile is not available on Android 10, I am using Android 12!

Maybe that is the case.

But a solution in LAWM may do exist or eventually be found, because this can be done in Java: https://medium.com/@eliothijanoc/simple-way-to-download-and-read-files-on-android-api-28-3557e6d240e5

Many thanks!

With warmest regards,


UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

c4p

  • Full Member
  • ***
  • Posts: 133
Re: How to download a file from web page displayed by jWebView
« Reply #68 on: June 09, 2023, 09:44:13 am »
Project folder might be in  /data/app/org.lamw.appname/files/Download  or /data/data/org.lamw.appname/files/Download on Android 10?
Lazarus 2.0.12 r64642/FPC 3.2.0 on Windows 10 22H2, projects mainly built using AppCompat and Gradle v6.6.1.

maurobio

  • Hero Member
  • *****
  • Posts: 611
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #69 on: June 09, 2023, 02:26:28 pm »
@Hi, c4p!

Quote
Project folder might be in  /data/app/org.lamw.appname/files/Download  or /data/data/org.lamw.appname/files/Download on Android 10?

There are no such folders in my device. If fact, I noticed that not only my app, but none of the LAMW demos have such folders.

I also noticed that I have many apps installed in my device, but just a fraction of them create these strange and horrible domain-like names. Anyway, I few of them do have such folders, therefore they may be created in Android 10.
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

c4p

  • Full Member
  • ***
  • Posts: 133
Re: How to download a file from web page displayed by jWebView
« Reply #70 on: June 09, 2023, 06:34:13 pm »
I would suggest adding a button to your app and include the following code on the onclick event, this will tell you the path of where the files should be located:

Code: Pascal  [Select][+][-]
  1. showmessage(self.GetEnvironmentDirectoryPath(dirDownloads));
Lazarus 2.0.12 r64642/FPC 3.2.0 on Windows 10 22H2, projects mainly built using AppCompat and Gradle v6.6.1.

maurobio

  • Hero Member
  • *****
  • Posts: 611
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #71 on: June 09, 2023, 07:50:33 pm »
Hi, @c3p!

The path displayed is:

Quote
/storage/emulated/0/Download

Hope this helps.

With warmest regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

c4p

  • Full Member
  • ***
  • Posts: 133
Re: How to download a file from web page displayed by jWebView
« Reply #72 on: June 09, 2023, 09:49:20 pm »
Oh boy, that's why you've been having all the trouble, although the StartDefaultActivityForFile may still be an issue, totally worth trying this again.
API differences for that environment variable between Android 10 & 12, on Android 10 (I honestly thought this would have changed in Android 10), dirDownloads means /Download, on Android 12 it means /Android/Data/org.lamw.appname/files/Download.
I may be wide of the mark, but this seems to make sense now.
The variable you 'probably' need is dirInternalAppStorage which probably goes to '/storage/emulated/0/data/user/0/org.lamw.appname' or '/data/user/0/org.lamw.appname' or something similar to that.
This means every time you have been downloading it's been going to /Download. which you can't launch a file from there without manually changing the device permissions, at least that's what I think's been happening.

Try the following code, you 'may' have to append +'/Download' to the dirInternalAppStorage environment variable, BUT (always a but) you will probably have to manually create the Download folder manually (it auto creates in Android 12 I think when you use dirDownloads) if it won't save to the root of the project dir, but I think it will (famous last words).

What's changed is dirDownloads has been changed to dirInternalAppStorage, that's all, might be easier to do a find/replace in your code.
If that does work, download and launches the files, you would have to check for Android versions <= Android 10 to use this environment and probably dirDownloads for > Android 10, anyone, one step at a time.

Code: Pascal  [Select][+][-]
  1. {hint: Pascal files location: ...\ReptileDatabase\jni }
  2. unit unit1;
  3.  
  4. {$mode delphi}
  5.  
  6. interface
  7.  
  8. uses
  9.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  10.   cthreads,
  11.   {$ENDIF}{$ENDIF}
  12.   Classes, SysUtils, StrUtils, AndroidWidget, menu, Laz_And_Controls, And_jni,
  13.   customdialog, broadcastreceiver, downloadmanager, intentmanager, downloadservice;
  14.  
  15. type
  16.  
  17.   { TAndroidModule1 }
  18.  
  19.   TAndroidModule1 = class(jForm)
  20.     jBroadcastReceiver1: jBroadcastReceiver;
  21.     jDownloadService1: jDownloadService;
  22.     jIntentManager1: jIntentManager;
  23.     jWebView1: jWebView;
  24.     jMenu1: jMenu;
  25.     jPanel1: jPanel;
  26.     jPanel2: jPanel;
  27.     jPanel3: jPanel;
  28.     jTextView2: jTextView;
  29.     jTextView3: jTextView;
  30.     jWebView1: jWebView;
  31.     Panel1: jPanel;
  32.     Panel2: jPanel;
  33.     TextView1: jTextView;
  34.     TextView2: jTextView;
  35.     TextView3: jTextView;
  36.     TextView4: jTextView;
  37.     TextView5: jTextView;
  38.     TextView6: jTextView;
  39.     TextView7: jTextView;
  40.     procedure AndroidModule1ClickOptionMenuItem(Sender: TObject;
  41.       jObjMenuItem: jObject; itemID: integer; itemCaption: string;
  42.       Checked: boolean);
  43.     procedure AndroidModule1CreateOptionMenu(Sender: TObject; jObjMenu: jObject);
  44.     procedure AndroidModule1Show(Sender: TObject);
  45.     procedure AndroidModule1SpecialKeyDown(Sender: TObject; keyChar: char;
  46.       keyCode: integer; keyCodeString: string; var mute: boolean);
  47.     procedure jBroadcastReceiver1Receiver(Sender: TObject; intent: jObject);
  48.     procedure jWebView1Status(Sender: TObject; Status: TWebViewStatus;
  49.       URL: String; var CanNavi: Boolean);
  50.   private
  51.     {private declarations}
  52.   public
  53.     {public declarations}
  54.   end;
  55.  
  56. var
  57.   AndroidModule1: TAndroidModule1;
  58.   DownloadURL, URLFileName, Extension: string;
  59.   res: TAndroidResult;
  60.  
  61. implementation
  62.  
  63. {$R *.lfm}
  64.  
  65.  
  66. { TAndroidModule1 }
  67.  
  68. procedure TAndroidModule1.jWebView1Status(Sender: TObject;
  69.   Status: TWebViewStatus; URL: String; var CanNavi: Boolean);
  70. var
  71.   SlashPos: integer;
  72. begin
  73.   //Pass the URL variable to our own variable to use in jDownloadReceiver later
  74.   DownloadURL := URL;
  75.   //Extract the file extension from the URL
  76.   Extension := ExtractFileExt(URL);
  77.   if (Extension = '.xlsx') or (Extension = '.xls') or (Extension = '.pdf') then
  78.    begin
  79.     //Get filename after the last slash in the URL using SlashPos position
  80.     SlashPos := LastDelimiter('/', URL); //Get the last occurrence of slash in the URL
  81.     //Get the actual filename so can use this later to save the file with the same name
  82.     URLFilename := AnsiRightStr(URL, Length(URL) - Slashpos);
  83.  
  84.     //Let's delete the file if it exists in case we cache an old file, as we will need to download the latest version of the file
  85.     if fileexists(Self.GetEnvironmentDirectoryPath(dirInternalAppStorage)+'/'+URLFilename) then
  86.      begin
  87.        Self.DeleteFile(self.GetEnvironmentDirectoryPath(dirInternalAppStorage)+'/'+URLFilename);
  88.      end;
  89.  
  90.       //Register Intent for download
  91.       jBroadcastReceiver1.RegisterIntentActionFilter('org.lamw.AppShowDown.DOWNLOAD_RECEIVER'); //Register intent for Broadcast receiver
  92.       //What your downloaded file will be called, let's use the URL filename (there may be a method to get the filename?)
  93.       //Save file to project Download directory
  94.       jDownloadService1.SaveToFile(Self.GetEnvironmentDirectoryPath(dirInternalAppStorage), URLFIlename);
  95.       //Start the download
  96.       jDownloadService1.Start(URL, 'org.lamw.AppShowDown.DOWNLOAD_RECEIVER');
  97.       //Let user know we have started downloading
  98.       ShowMessage('Downloading file...please wait');
  99.    end;
  100. end;
  101.  
  102. procedure TAndroidModule1.jBroadcastReceiver1Receiver(Sender: TObject;
  103.   intent: jObject);
  104. begin
  105.          //Store result code so we can check later if the download was successful
  106.          res := jBroadcastReceiver1.GetResultCode();
  107.          //Use res to check for successful download or fail
  108.          if (res = RESULT_OK) then
  109.           begin
  110.            ShowMessage('Download of' + sLineBreak + DownloadURL + sLineBreak + 'successful');
  111.            //Launch the file once it has downloaded
  112.             if (Extension = '.xls') then        // alternative office document mime type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  113.              begin
  114.               Self.StartDefaultActivityForFile(Self.GetEnvironmentDirectoryPath(dirInternalAppStorage) + '/'+ URLFIlename,'application/vnd.ms-excel'); //Start the downloaded file using Excel
  115.              end
  116.             else if (Extension = '.xlsx') then
  117.              begin
  118.               Self.StartDefaultActivityForFile(Self.GetEnvironmentDirectoryPath(dirInternalAppStorage) + '/'+ URLFIlename,'application/vnd.ms-excel'); //Start the downloaded file using Excel
  119.              end
  120.             else if (Extension = '.pdf') then
  121.               begin
  122.                Self.StartDefaultActivityForFile(Self.GetEnvironmentDirectoryPath(dirInternalAppStorage) + '/'+ URLFIlename,'application/pdf'); //Start the downloaded file using PDF VIewer
  123.               end;
  124.            end
  125.              else if (res <> RESULT_OK) then
  126.               begin
  127.                ShowMessage('Download of' + sLineBreak + DownloadURL + sLineBreak + 'FAILED!');
  128.               end;
  129.  
  130.    //unregister BroadcastReceiver ...
  131.    jBroadcastReceiver1.Unregister();
  132. end;
  133.  
  134. procedure TAndroidModule1.AndroidModule1Show(Sender: TObject);
  135. begin
  136.     jWebView1.Navigate('http://biotupe.org/test/');
  137. end;
  138.  
  139. procedure TAndroidModule1.AndroidModule1SpecialKeyDown(Sender: TObject;
  140.   keyChar: char; keyCode: integer; keyCodeString: string; var mute: boolean);
  141. begin
  142.     if (KeycodeString = 'KEYCODE_BACK') then
  143.   begin
  144.     if jWebView1.CanGoBack then
  145.       jWebView1.GoBack;
  146.     mute := True;
  147.   end;
  148. end;
  149.  
  150. procedure TAndroidModule1.AndroidModule1CreateOptionMenu(Sender: TObject;
  151.   jObjMenu: jObject);
  152. var
  153.   i: integer;
  154. begin
  155.   for i := 0 to jMenu1.Options.Count - 1 do
  156.     jMenu1.AddItem(jObjMenu, i, jMenu1.Options.Strings[i], '',
  157.       mitDefault, misNever);
  158. end;
  159.  
  160. procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);
  161. begin
  162.   jWebView1.SetLoadWithOverviewMode(True);
  163.   jWebView1.SetUseWideViewPort(True);
  164. end;
  165.  
  166. procedure TAndroidModule1.AndroidModule1ClickOptionMenuItem(Sender: TObject;
  167.   jObjMenuItem: jObject; itemID: integer; itemCaption: string; Checked: boolean);
  168. begin
  169.   case itemID of
  170.     0:
  171.     begin
  172.       ShowMessage('The ShowDown App' + sLineBreak +
  173.         'This app demonstrates how to download and open' + sLineBreak +
  174.         'a linked file from a webpage with' + sLineBreak +
  175.         'the associated app' + sLineBreak + sLineBreak +
  176.         'This code does not belongs to anyone. Property is theft. Help yourself.',
  177.         gvCenter, slLong);
  178.     end;
  179.     1:
  180.       AndroidModule1.Close;
  181.   end;
  182. end;
  183.  
  184. end.
Lazarus 2.0.12 r64642/FPC 3.2.0 on Windows 10 22H2, projects mainly built using AppCompat and Gradle v6.6.1.

maurobio

  • Hero Member
  • *****
  • Posts: 611
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #73 on: June 10, 2023, 02:15:20 pm »
Hi, @c4p!

I have bad news... The modified code did not work - in fact, it seems to be a step backward in comparison to a previous version which used the DownloadManager component instead of DownloadService, because now the file is not even downloaded!

Shouldn't the directory '/data/user/0/org.lamw.appname/' exist? Or be created at a location in the file system which can be accessed by the app?

Here are the values of dirDownloads and dirInternalAppStorage in my system:

Quote
dirDownloads = /storage/emulated/0/Download

Quote
dirInternalAppStorage = /data/user/0/org.lamw.appname/files

(where appname is the name of my app)

I suggest that we forgot about DownloadService and work with DownloadManager instead; also, we could use a fixed directory name instead of dirDownloads or dirInternalAppStorage or whatever Android throws at us, which should be created by the app beforehand (in case it does not already exist). May this be possible?

With warmest regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

c4p

  • Full Member
  • ***
  • Posts: 133
Re: How to download a file from web page displayed by jWebView
« Reply #74 on: June 10, 2023, 02:39:31 pm »
OK, hold up, I found an Android 10 phone, I will do some tests.
Lazarus 2.0.12 r64642/FPC 3.2.0 on Windows 10 22H2, projects mainly built using AppCompat and Gradle v6.6.1.

 

TinyPortal © 2005-2018