Recent

Author Topic: [LAMW] jFileProvider cannot read file from assets subfolder  (Read 1670 times)

Manlio

  • Full Member
  • ***
  • Posts: 164
  • Pascal dev
[LAMW] jFileProvider cannot read file from assets subfolder
« on: April 30, 2019, 12:29:13 am »
Hello

If I put a text file in the assets folder (e.g. test.txt) then I can get it like this:

    s := jFileProvider.GetTextContent('test.txt');

And this works fine.

However, if I put the same file in a subfolder of the assets folder, e.g. sub/test.txt, and I try to read it like this:

    s := jFileProvider.GetTextContent('sub/test.txt');

Then it doesn't work. s comes back empty.

BTW I looked inside the compiled APK, and I can see the file there, e.g. assets/sub/test.txt so the file is for sure in the APK, but I cannot get it using jFileProvider.GetTextContent

Am I doing something wrong? Should I write the path differently? (I tried a lot of variations of the path, with and without / but I could not get it to find that file...)

Any help will be very appreciated!

All the best,

manlio mazzon gmail

Manlio

  • Full Member
  • ***
  • Posts: 164
  • Pascal dev
I finally managed to solve the problem. In order to be able to read files in subfolders of assets it is necessary to use AssetManager instead of ContentResolver (in jFileProvider.java)

In the attached file is a patch for fileprovider.pas and jFileProvider.java that allows to ready files from assests subfolders. I hope the explanations included in it are enough for anyone who needs them, but if not, please write me here.

And if jmpessoa wishes to include that in LAMW, by all means do so in any way.

All the best and many thanks to everyone.
manlio mazzon gmail

jmpessoa

  • Hero Member
  • *****
  • Posts: 2301
Re: [LAMW] jFileProvider cannot read file from assets subfolder
« Reply #2 on: May 06, 2019, 07:48:12 am »
this also works ....

Code: Pascal  [Select][+][-]
  1. s := jTextFileManager1.GetTextContent('sub/test.txt');
  2.  

About  the "jFileProvider" there are some more "conceptualization", 
We need "provide" file to others  Apps.  [see demo "AppFileProviderClientDemo1"]

here a good ref.   https://codeexa.com/android-how-to-read-share-file-from-assets/

In this sense,  I think that your contribution can be used to improve
jTextFileManager or  jForm utils....

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

Manlio

  • Full Member
  • ***
  • Posts: 164
  • Pascal dev
Re: [LAMW] jFileProvider cannot read file from assets subfolder
« Reply #3 on: May 06, 2019, 09:15:02 pm »
Thanks, I didn't know about that use of jTextFileManager.

In my case however I needed to get binary files, so I still needed a solution that returned a TMemoryStream.

I also guess that you would prefer that I put my patches on github also? In that way maybe it's easier for you to take something that you might find useful? My problem is that I still don't know how to use Git, but as soon as I can I will start to put my stuff there too, for anyone who might want it.

manlio mazzon gmail

jmpessoa

  • Hero Member
  • *****
  • Posts: 2301
Re: [LAMW] jFileProvider cannot read file from assets subfolder
« Reply #4 on: May 06, 2019, 09:32:27 pm »

Quote
In my case however I needed to get binary files, so I still needed a solution that returned a TMemoryStream.

OK. I will commit your code to  jTextFileManager component!

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

jmpessoa

  • Hero Member
  • *****
  • Posts: 2301
Re: [LAMW] jFileProvider cannot read file from assets subfolder
« Reply #5 on: May 06, 2019, 11:25:57 pm »
Done!!

    Improved  jTextFileManager component

    added:
 
Code: Pascal  [Select][+][-]
  1.     function LoadFromAssetsAsJByteArray(_filePath: string): TDynArrayOfJByte;
  2.     function LoadFromAssetsAsStream(_filePath: string): TMemoryStream;
  3.     function LoadFromAssetsAsText(_filePath: string; IsUTF8: boolean): string;
  4.  

Thank you!
 
« Last Edit: May 06, 2019, 11:29:21 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

 

TinyPortal © 2005-2018