Recent

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

maurobio

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

I tried this:

Code: Pascal  [Select][+][-]
  1. if not DirectoryExists(TempDir) then
  2.     begin
  3.       if not CreateDir(TempDir) then
  4.       begin
  5.         ShowMessage('Error creating temporary directory');
  6.         Exit;
  7.       end
  8.       else
  9.       CreateDir(TempDir);
  10.       CopyFile(ExtractFileName(URLFilename), TempDir + ExtractFilename(URLFilename));
  11.     end;

where "TempDir" is a string constant declated as:

Code: Pascal  [Select][+][-]
  1. const
  2.   TempDir = '/Android/data/org.lamw.showdown/files/';

But this does not work either - no directory is created, no error message is issued, and of course no file is copied to anywhere.

I don't even see something like '/Android/data/org.lamw.MYAPPNAME' in my directory tree (either in the main storage or the SD card).

Unless you wizards appear with a complete piece of software which fully works, hopefully based on the demo I provided in a previous post, I gave up. I expected this to be an exceedingly simple matter, but so far it has turned out to be only a wasteful nightmare.  >:(

Maybe some things are better done in Java, or not at all.

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 #46 on: June 07, 2023, 02:35:11 pm »
Hi, @c4p, @jmpessoa!

Since I have not heard any more from you, it is possible that you had gave up too or considered that the problem has been solved - which, for me, has been not.

I stumbled upon this: https://medium.com/@eliothijanoc/simple-way-to-download-and-read-files-on-android-api-28-3557e6d240e5, which as far as I could understand (the problem is not English, it is Java!) provides an easier solution to accessing files in the Download directory of Android versions 10+. And if it can be done in Java, I can see no good reason why it cannot be done in LAMW as well.

As an aside, I would like to ask: should my app create a directory somewhere in the file system like "org.lamw.myapp" (where "myapp" is the name of my app)? I could not find any such directory anywhere. s it created only programmatically?

I will do keep trying...

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: 611
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #47 on: June 07, 2023, 04:31:25 pm »
Hi, @c4p, @jmpessoa!

Just to sum up my attempts up to now considering your recent comments & suggestions

Quote
It's definitely a permissions issue, if you go to your app in Settings on Android, go to Apps select your app and go to Permissions, then Files and Media, it's probably set to 'Allow access to media only' select 'Allow management of all files'.

My device has no such "Files and Media" option under Settings/Apps and Notifications/Permissions, but it has instead a "Memory" option which I checked to give permission.

Quote
This works now on my device after I set the permissions manually on the device to Allow management of all files:
Self.StartDefaultActivityForFile(Self.GetEnvironmentDirectoryPath(dirSdCard)+'/Download/'+URLFilename,'application/vnd.ms-excel');

It does not work for me. Result is the same: nothing happens, no error or warning message is issued.

Quote
@maurobio probably something like this for your app using excel mime type, obviously you would need to do a check to see if the downloaded file is excel, pdf, txt extension etc. and assign correct mime type for the downloaded file type.

BroadcastReceiver1.RegisterIntentActionFilter('org.lamw.YOURAPPNAME.DOWNLOAD_RECEIVER'); //Register intent for Broadcast receiver
DownloadService1.SaveToFile(Self.GetEnvironmentDirectoryPath(dirDownloads), URLFilename); //Save file to project Download directory
DownloadService1.Start(URL, 'org.lamw.YOURAPPNAME.DOWNLOAD_RECEIVER'); //Start the download

Using DownloadService instead of DownloadManager as before does not change anything. The file is correcelty downloaded, but still nothing happens - no warnings or error messages.

Quote
Put below into OnReceiver event of BroadcastReceiver.
Self.StartDefaultActivityForFile(Self.GetEnvironmentDirectoryPath(dirDownloads) + '/sample-1.xlsx','application/vnd.ms-excel');

Putting that code in the OnReceiver event does not changes anything. Again, not happens: no warnings, no error messages.

Last, but not least, jmpessoa stated that:

Quote
Yes! the demo " AppDownloadManagerDemo1"  has all we need....    

Not, it does not. The demo downloads an image from a website, but then displays it on a jImageView component, when if fact what "we need" would be the image opened by an external app. If that demo could be modified to do this, then I suppose it could be realistically used as the bases of a solution to the problem.

Hope this helps (but do not believe it will).

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: 2249
Re: How to download a file from web page displayed by jWebView
« Reply #48 on: June 07, 2023, 10:13:41 pm »
Quote
The demo downloads an image from a website, but then displays it on a jImageView component, when if fact what "we need" would be the image opened by an external app....

Replace the jImageView  reference by:

Code: Pascal  [Select][+][-]
  1.     IntentManager1.SetAction(iaView);
  2.     IntentManager1.SetDataAndType(jDownloadManager1.GetFileUri(), 'application/pdf');  //application/vnd.ms-excel ???
  3.     IntentManager1.StartActivity('Open with?');  
  4.  
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

maurobio

  • Hero Member
  • *****
  • Posts: 611
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #49 on: June 07, 2023, 10:22:36 pm »
Hi, @jmpessoa!

I didn't understand this:

Code: Pascal  [Select][+][-]
  1. IntentManager1.StartActivity('Open with?');

Please be more specific. What exactly should be passed as parameter to StartActivity?

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: 2249
Re: How to download a file from web page displayed by jWebView
« Reply #50 on: June 07, 2023, 10:42:10 pm »
Quote
Open with?

A simple message...... so, user can select the best appp [suggestion] to open the file... 

If you will test it using "AppDownloagManageDemo1",  take care  of the URL and file mimetype   (as is, use "image/*")
« Last Edit: June 07, 2023, 10:48:09 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

maurobio

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

OK, I will try it (with the AppDownloadManagerDemo1, for sure) and will post the results ASAP.

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 #52 on: June 08, 2023, 12:00:11 am »
Hi, @jmpessoa!

I had yet no success in displaying the downloaded file. The download is successful and for pdf files the associated app is even called, but the document is not displayed. In the case of Excel files, a Java error message is raised.

I attach the modified demo file. Please compile and run ii and take a look at the source to see if the mods I implemented (in jButton1Click event and jBroadcastReceiver1Receiver event) are correct.

Thank you.

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: 2249
Re: How to download a file from web page displayed by jWebView
« Reply #53 on: June 08, 2023, 01:18:41 am »
sorry... FIXED!  --> GetFileUri(intent)  not   GetFileUri()

Code: Pascal  [Select][+][-]
  1.   IntentManager1.SetAction(iaView);
  2.   IntentManager1.SetDataAndType(jDownloadManager1.GetFileUri(intent) , 'application/pdf');  //application/vnd.ms-excel ???
  3.   IntentManager1.StartActivity('Open with?');  
  4.  

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

maurobio

  • Hero Member
  • *****
  • Posts: 611
  • Ecology is everything.
    • GitHub
Re: How to download a file from web page displayed by jWebView
« Reply #54 on: June 08, 2023, 01:31:21 am »
Hi, @jmpessoa!

No error message this time - the file is downloaded, but, again, it is not opened and nothing happens...

It is as if the StartActivity() method is never called.

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: 2249
Re: How to download a file from web page displayed by jWebView
« Reply #55 on: June 08, 2023, 01:44:04 am »

Here is my testd code:

Code: Pascal  [Select][+][-]
  1. {Hint: save all files to location: C:\adt32\eclipse\workspace\AppDownloadManagerDemo1\jni }
  2. unit unit1;
  3.  
  4. {$mode delphi}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, And_jni, And_jni_Bridge, Laz_And_Controls,
  10.   Laz_And_Controls_Events, AndroidWidget, downloadmanager, broadcastreceiver,
  11.   intentmanager;
  12.  
  13. type
  14.  
  15.   { TAndroidModule1 }
  16.  
  17.   TAndroidModule1 = class(jForm)
  18.     ImageView1: jImageView;
  19.     IntentManager1: jIntentManager;
  20.     jBroadcastReceiver1: jBroadcastReceiver;
  21.     jButton1: jButton;
  22.     jCheckBox1: jCheckBox;
  23.     jDownloadManager1: jDownloadManager;
  24.     jTextView1: jTextView;
  25.     procedure AndroidModule1JNIPrompt(Sender: TObject);
  26.     procedure AndroidModule1RequestPermissionResult(Sender: TObject;
  27.       requestCode: integer; manifestPermission: string;
  28.       grantResult: TManifestPermissionResult);
  29.     procedure jBroadcastReceiver1Receiver(Sender: TObject; intent: jObject);
  30.     procedure jButton1Click(Sender: TObject);
  31.   private
  32.     {private declarations}
  33.   public
  34.     {public declarations}
  35.   end;
  36.  
  37. var
  38.   AndroidModule1: TAndroidModule1;
  39.  
  40. implementation
  41.  
  42. {$R *.lfm}
  43.  
  44.  
  45. { TAndroidModule1 }
  46.  
  47. procedure TAndroidModule1.jButton1Click(Sender: TObject);
  48. var
  49.   res: TAndroidResult;
  50. begin
  51.  
  52.     //hint: if you  get "write" permission then you have "read", too!
  53.    if not IsRuntimePermissionGranted('android.permission.WRITE_EXTERNAL_STORAGE') then
  54.    begin
  55.      ShowMessage('Sorry... Some Runtime Permission NOT Granted ...');
  56.      Exit;
  57.    end;
  58.  
  59.    if not Self.isConnected() then
  60.    begin
  61.      ShowMessage('Sorry,  Device is not connected...');
  62.      Exit;
  63.    end;
  64.  
  65.   jBroadcastReceiver1.RegisterIntentActionFilter(jDownloadManager1.GetActionDownloadComplete()); //'android.intent.action.DOWNLOAD_COMPLETE'
  66.  
  67.   jDownloadManager1.SaveToFile(dirDownloads, 'example12.pdf'); //http://clipart-library.com/images/6Tp5qzgnc.jpg
  68.   res:= jDownloadManager1.Start('https://od.lk/d/Ml8yMTQ3MzU1MDhf/example.pdf');
  69.  
  70.   if res = RESULT_OK then
  71.     ShowMessage('OK!')
  72.   else
  73.     ShowMessage('FAIL');
  74.  
  75. end;
  76.  
  77. procedure TAndroidModule1.jBroadcastReceiver1Receiver(Sender: TObject; intent: jObject);
  78. begin
  79.  
  80.   (*
  81.   ShowMessage(jDownloadManager1.GetExtras(intent, '|'));  //MUST! be call first!
  82.  
  83.   ShowMessage(jDownloadManager1.GetLocalFileName());
  84.   ShowMessage('Size(bytes): ' + IntToStr(jDownloadManager1.GetFileSizeBytes()) );
  85.   ShowMessage(jDownloadManager1.GetMediaType());
  86.   ShowMessage(jDownloadManager1.GetLocalUriAsString());
  87.  
  88.                                //or Self.ParseUri(jDownloadManager1.GetLocalUriAsString())
  89.   jImageView1.SetImageFromURI( jDownloadManager1.GetFileUri() );
  90.   *)
  91.  
  92.   //or
  93.  
  94.   //jImageView1.SetImageFromURI( jDownloadManager1.GetFileUri(intent) );
  95.  
  96.   //or
  97.   IntentManager1.SetAction(iaView);
  98.   IntentManager1.SetDataAndType(jDownloadManager1.GetFileUri(intent) , 'application/pdf');  //application/vnd.ms-excel ???
  99.   IntentManager1.StartActivity('Open with?');
  100.  
  101. end;
  102.  
  103. procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);
  104. var
  105.   manifestPermissions: array of string;
  106. begin
  107.   if not Self.isConnected() then
  108.   begin //try wifi
  109.     if Self.SetWifiEnabled(True) then
  110.       jCheckBox1.Checked:= True
  111.     else
  112.       ShowMessage('Please,  try enable some connection...');
  113.   end
  114.   else
  115.   begin
  116.      if Self.isConnectedWifi() then jCheckBox1.Checked:= True
  117.   end;
  118.  
  119.   if IsRuntimePermissionNeed() then   // that is, target API >= 23  - Android 6
  120.   begin
  121.      ShowMessage('RequestRuntimePermission....');
  122.  
  123.      SetLength(manifestPermissions, 1);
  124.  
  125.      //hint: if you  get "write" permission then you have "read", too!
  126.      manifestPermissions[0]:= 'android.permission.WRITE_EXTERNAL_STORAGE'; //from AndroodManifest.xml
  127.      Self.RequestRuntimePermission(manifestPermissions, 701);
  128.  
  129.      SetLength(manifestPermissions, 0);
  130.   end;
  131.  
  132.  
  133. end;
  134.  
  135. procedure TAndroidModule1.AndroidModule1RequestPermissionResult(
  136.   Sender: TObject; requestCode: integer; manifestPermission: string;
  137.   grantResult: TManifestPermissionResult);
  138. begin
  139.   case requestCode of
  140.      701:begin
  141.               if grantResult = PERMISSION_GRANTED  then
  142.                 ShowMessage('Success! ['+manifestPermission+'] Permission grant!!! ' )
  143.               else  //PERMISSION_DENIED
  144.                 ShowMessage('Sorry... ['+manifestPermission+'] Permission not grant... ' );
  145.           end;
  146.   end;
  147. end;
  148.  
  149. end.
  150.  
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

c4p

  • Full Member
  • ***
  • Posts: 133
Re: How to download a file from web page displayed by jWebView
« Reply #56 on: June 08, 2023, 09:52:09 am »
Quote
My device has no such "Files and Media" option under Settings/Apps and Notifications/Permissions, but it has instead a "Memory" option which I checked to give permission.

I am on Android 12, if you are on Android 10, you will see different options:
Open Settings and navigate to Apps & notifications. From there, tap the Advanced option and then tap Permission Manager.
You’ll see a list of all the various permissions on your device. Select your app to view the permissions, at least that's what it should be.
Android 10 permissions have different categories. They are filed under Allowed or Denied.

Quote
It does not work for me. Result is the same: nothing happens, no error or warning message is issued.

See permissions access in your Android 10 device, as above.

Quote
Using DownloadService instead of DownloadManager as before does not change anything. The file is correcelty downloaded, but still nothing happens - no warnings or error messages.

Using jDownloadService was a suggestion to download direct to your project folder, jDownloadManager will not do this in it's current form AFAIK. I said this to get around the permissions issue but I think you said you want both options to work, so probably a moot point now. I would try and at least get one option working, then focus on getting the other one working, at least, that's what I would do.
CopyFile will only work if you have permissions too, if you are wanting to copy from /Download to project folder 'Download', permissions are still required for the copy, same as open file.
I don't know what your app actually does other than download a file from a website, presumably you want the user to have access to the downloaded file in /Download and open then permissions required.

Quote
Putting that code in the OnReceiver event does not changes anything. Again, not happens: no warnings, no error messages.

This only works if you're using the jDownloadService and downloading direct to your Project 'Download' folder NOT from /Download. This was to workaround the permissions, but I think you said you want to resolve the permissions issue. It was a suggestion to at least getting a working prototype.
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 #57 on: June 08, 2023, 02:59:15 pm »
Hi, @BobDob!

My problem is not to download the file, this I do without issues. My problem is to have it opened by the associated app once it is downloaded!

Obviously, in such case a "console app" will not be useful.

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 #58 on: June 08, 2023, 03:01:10 pm »
Hi, @jmpessoa!

Your tested code is identical to mine. It does not work. The file is downloaded, but it is not opened in the associated app as expected.

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 #59 on: June 08, 2023, 03:07:38 pm »
Hi, @c4p!

Quote
I don't know what your app actually does other than download a file from a website, presumably you want the user to have access to the downloaded file in /Download and open then permissions required.

My app is an extremely simple one: is just display a website where there are links to files in .xlsx and .pdf formats. What I want is simply have each file downloaded, when the link to that file is clicked, to any folder where it can be opened by the associated app!, Simple as that. But it is proving to be a nightmare.  >:(

I don't care if this is done using DownloadManager, DownloadService, or a magic wand - I just want that such a simple task, done by thousands of apps, work in my app!

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

 

TinyPortal © 2005-2018