Recent

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

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #15 on: June 04, 2023, 07:51:37 pm »
Hi, @jmpessoa!

Quote
Test it for "application/pdf"  first!

Why should it work with a specific mime type and not with any other? This demands a clear, comprehensive explanation.

Quote
Android 11  - Api 30  (and up) has restricted developers to access devices folders (ex: "Download, Documents, ..., etc")"

If the problem with opening the downloaded file is caused by such "restrictions", why is it that my app can save files in the Download folder without any hassles? An why is it that the OS does not issue any error or warning messages when I attempt to open a file using an Intent? Furthermore (because there is always "more"!) why is it that thousands of apps can easily save and open files in the Downloads folder (notwithstanding the Android version running in the device)? Anyway, why should this be expected to work with "pdf" files, but not "xlsx" ones?

Quote
the demo is here: "AppPublicFoldersAccessDemo1'

Referring constantly to code deeply buried in the LAMW demos - in the vague hope that one can see a solution there - does not really helps. Working fragments of code, posted here, this is what helps.

Best regards,
« Last Edit: June 04, 2023, 07:53:18 pm by maurobio »
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

jmpessoa

  • Hero Member
  • *****
  • Posts: 2296
Re: How to download a file from web page displayed by jWebView
« Reply #16 on: June 04, 2023, 08:04:39 pm »
Quote
Why should it work with a specific mime type and not with any other?

yes, if it work,  it wil work  with any other mime type.... but for test.......
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #17 on: June 04, 2023, 08:29:01 pm »
Hi, @jmpessoa!

Quote
yes, if it work,  it will work  with any other mime type.... but for test.......

Sorry, but this looks suspiciously like "voodoo programming" (https://www.techopedia.com/definition/22860/voodoo-programming), which just leads one to wasting time and suffering unnecessary rises in blood pressure.

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

jmpessoa

  • Hero Member
  • *****
  • Posts: 2296
Re: How to download a file from web page displayed by jWebView
« Reply #18 on: June 04, 2023, 08:35:56 pm »

No,that is "programming" .....

you have a complex mime type:  "vnd.openxmlformats-officedocument.spreadsheetml.sheet",
do you know if this is correct?  I don't know....

So, test the simple, after the complex....
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #19 on: June 04, 2023, 09:33:35 pm »
Hi, @jmpessoa!

Quote
No,that is "programming" .....

Per the definition of "voodoo programming":

Quote
Voodoo programming refers to the practice of coding according to superstition, guesses, or anything other than logic. Voodoo programming is a rather broad term for situations where a programmer uses a piece of code without truly understanding how it works.

... we are clearly practising it.

Quote
you have a complex mime type:  "vnd.openxmlformats-officedocument.spreadsheetml.sheet",
do you know if this is correct?  I don't know....

Of course it is "correct", as much as any other (see the list of available mime types here: https://android.googlesource.com/platform/external/mime-support/+/9817b71a54a2ee8b691c1dfa937c0f9b16b3473c/mime.types). What a purely subjective impression of "complexity" has to do with it?

BTW, see here an example in Java of the use of that mime type:

Code: Java  [Select][+][-]
  1. Intent intent;
  2.    
  3.         String[] mimetypes =
  4.                 { "application/vnd.ms-excel", // .xls
  5. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" // .xlsx
  6.                        };
  7.  
  8.         intent = new Intent(Intent.ACTION_GET_CONTENT); // or use ACTION_OPEN_DOCUMENT
  9.         intent.setType("*/*");
  10.         intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
  11.         intent.addCategory(Intent.CATEGORY_OPENABLE);
  12.  

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

jmpessoa

  • Hero Member
  • *****
  • Posts: 2296
Re: How to download a file from web page displayed by jWebView
« Reply #20 on: June 04, 2023, 09:39:32 pm »
Quote
see here an example in Java of the use of that mime type...

Good! Now I know about!

Thank you!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
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

jmpessoa

  • Hero Member
  • *****
  • Posts: 2296
Re: How to download a file from web page displayed by jWebView
« Reply #22 on: June 04, 2023, 10:31:34 pm »
Ok. I will try!

until then check your project file "AndroidManifes.xml", look for:

Quote
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion"  tools:ignore="ScopedStorage"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
    <uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>
    <uses-permission android:name="android.permission.READ_MEDIA_AUDIO"/>

and try to add (one by one) them...
« Last Edit: June 04, 2023, 10:34:07 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

c4p

  • Full Member
  • ***
  • Posts: 157
Re: How to download a file from web page displayed by jWebView
« Reply #23 on: June 04, 2023, 10:33:14 pm »
OK, all of these mime types work for me using StartDefaultActivityForFile, but NOT to the root /Download directory or dirInternalAppStorage but work fine from the project Download directory (dirDownloads), PDF and Excel file open direct from the filesystem without issue. Can download fine to /Download normally using jDownloadManager though.
Check this again maurobio to see if it works? thank you.

Code: Pascal  [Select][+][-]
  1. Self.StartDefaultActivityForFile(Self.GetEnvironmentDirectoryPath(dirDownloads)+'/file.pdf', 'application/pdf');
  2.  
  3. Self.StartDefaultActivityForFile(Self.GetEnvironmentDirectoryPath(dirDownloads)+'/file.xlsx','application/vnd.ms-excel');
  4. Self.StartDefaultActivityForFile(Self.GetEnvironmentDirectoryPath(dirDownloads)+'/file.xlsx','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
« Last Edit: June 04, 2023, 10:49:22 pm by c4p »
Lazarus 2.0.12 r64642/FPC 3.2.0 LAMW v0.8.6.4 on Windows 10+Linux Mint 21.2, projects mainly built using AppCompat and Gradle v8.5

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #24 on: June 04, 2023, 10:45:14 pm »
Hi, @jmpessoa!

My project AndroidManifest.xml already included much of the flags you have suggested; adding the others did not made any difference.

I doubt this is a problem of permissions, because: (i) I can save (write) to the main storage Downloads folder without problems; and (ii) I get no error or warning messages from Android - in fact, nothing happens after the file is correctly downloaded (when it should then be opened in the associated application).

Since you feel at will with demos, I can suggest you create a small sample with two buttons, one for displaying a XLSX file, the other for displaying a PDF file; put these files in the your main storage Downloads folder and let's see what happens. As a start, I attach an (incomplete) app based on one of the LAMW demos. If it works, the problem is solved and you will be able to add another useful demo to the repository (therefore aiding others in the future).

Hope this helps.

With 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

jmpessoa

  • Hero Member
  • *****
  • Posts: 2296
Re: How to download a file from web page displayed by jWebView
« Reply #25 on: June 04, 2023, 11:02:42 pm »

Well, you "demo" not handle permission by the user.... what about?
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

maurobio

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

Quote
OK, all of these mime types work for me using StartDefaultActivityForFile, but NOT to the root /Download directory or dirInternalAppStorage but work fine from the project Download directory (dirDownloads), PDF and Excel file open direct from the filesystem without issue. Can download fine to /Download normally using jDownloadManager though.

Your suggestions did not work because, if I did understand correctly, the file is (correctly) downloaded to the main storage but (always a "but"!) it can only be opened if it is downloaded to the app Download folder.

Here is the situation of my code until now, it need to be modified so that the file can be downloaded and opened without issues!

Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.jWebView1Status(Sender: TObject; Status: TWebViewStatus;
  2.   URL: string; var CanNavi: boolean);
  3. var
  4.   res: TAndroidResult;
  5.   SlashPos: integer;
  6.   URLFileName: string;
  7. begin
  8.   if ExtractFileExt(URL) = '.xlsx'
  9.     //Can check in an array if you want with a bunch of extensions or another way?
  10.   begin
  11.     SlashPos := LastDelimiter('/', URL); //Get the last occurrence of slash in the URL
  12.     URLFilename := AnsiRightStr(URL, Length(URL) - Slashpos);
  13.    //Check if file already exists to avoid downloading multiple versions of it
  14.     if not FileExists(self.GetEnvironmentDirectoryPath(dirDownloads) +
  15.       '/' + URLFilename) then
  16.     begin
  17.       //Get filename after the last slash in the URL using SlashPos position
  18.       jBroadcastReceiver1.RegisterIntentActionFilter(
  19.         jDownloadManager1.GetActionDownloadComplete()); //Register intent
  20.       jDownloadManager1.SaveToFile(URLFIlename);
  21.       //What your downloaded file will be called, let's use the URL filename (there may be a method to get the filename?)
  22.       res := jDownloadManager1.Start(URL); //Start the download
  23.       //Use res to check for successful download or fail
  24.       if (res = RESULT_OK) then
  25.         ShowMessage('Download of' + sLineBreak + URL + sLineBreak + 'successful')
  26.       else
  27.         ShowMessage('Download of' + sLineBreak + URL + sLineBreak + 'FAILED!');
  28.     end;
  29.  
  30.    
  31. jIntentManager1.SetAction('android.intent.action.VIEW');
  32.    // This does not work (i.e., nothing happens)
  33.     StartDefaultActivityForFile(Self.GetEnvironmentDirectoryPath(
  34.       dirDownloads) + ExtractFileName(URLFilename),
  35.       'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  36.   end;
  37. end;
  38.  
  39.  

With 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: 623
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #27 on: June 04, 2023, 11:11:55 pm »
Hi, @jmpessoa!

Quote
Well, you "demo" not handle permission by the user.... what about?

This "demo" is incomplete and not tested; I provided it just a starting point.

Anyway, see the previous message by c4p.

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

jmpessoa

  • Hero Member
  • *****
  • Posts: 2296
Re: How to download a file from web page displayed by jWebView
« Reply #28 on: June 05, 2023, 01:27:34 am »
@c4p
Quote
but NOT to the root /Download directory or dirInternalAppStorage but work fine from the project Download directory (dirDownloads)

Yes, for Android 11 (and Up) .... this is it! 

It is very likely that the solution suggested so far works only for Android < 11 ....

Hi,  c4p!

Please, study the demo  "AppPublicFoldersAccessDemo1", 
maybe you can make a new one much simpler to meet the @maurobio requirement...
(that is, I'm guessing you already understand what actually solves the question.....)

« Last Edit: June 05, 2023, 01:30:45 am by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #29 on: June 05, 2023, 02:04:49 am »
Hi, @jmpessoa, @c4p!

Thanks a lot for your hopefully inexaustible patience in helping me!  :D

Quote
It is very likely that the solution suggested so far works only for Android < 11 ....

Probably Android < 10 because it does not work for me (my device runs Android 10).

I have not looked at the demo suggested by jmpessoa but hope to do it by tomorrow.

Hope a solution to this annoying problem come up soon...

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

 

TinyPortal © 2005-2018