Lazarus

Programming => Operating Systems => Android => Topic started by: neuro on July 26, 2021, 03:46:00 am

Title: jOpenDialog crashes in Android 10
Post by: neuro on July 26, 2021, 03:46:00 am
The sample project “AppOpenFileDialogDemo1”
works in Android 4.3 (32-bit, device: Samsung Galaxy S3),
however fails to work in Android 10 (64-bit, device: Samsung Galaxy S9).
In sample project both buttons “Get File List” and “jOpenDialog1.Show()” crash the app when running in Android 10.
The following code also crashes in Android 10:
Code: Pascal  [Select][+][-]
  1. listFile:= Self.GetFileList(Self.GetEnvironmentDirectoryPath(dirDownloads));

How to fix this bug of jOpenDialog?
Title: Re: jOpenDialog crashes in Android 10
Post by: jmpessoa on July 26, 2021, 04:06:41 am

Maybe you need handle file permissions....

(Please,  see jCameraDemo or others..... how to handle file permissions....)
Title: Re: jOpenDialog crashes in Android 10
Post by: neuro on July 26, 2021, 04:28:04 am
“AppCameraDemo” also fails to work in Android 10.
The app writes “'Success! ['+manifestPermission+'] Permission grant!!!”,
however when the button “Take Photo!” is pressed, the app crashes.
Just try it yourself in Android 10.
Title: Re: jOpenDialog crashes in Android 10
Post by: jmpessoa on July 26, 2021, 05:10:42 am
Ok... but you can get from "AppCameraDemo" the code to handle file permission...
Title: Re: jOpenDialog crashes in Android 10
Post by: neuro on July 26, 2021, 05:18:30 am
File permissions are invoked and granted successfully,
however file permissions do not help – the app crashes in aarch64 Android 10.
Do you have aarch64 Android 10 device for testing “AppOpenFileDialogDemo1” and "AppCameraDemo"?
On which device you have done the testing?
Title: Re: jOpenDialog crashes in Android 10
Post by: jmpessoa on July 26, 2021, 05:28:54 am

Quote
aarch64...

No...

Try "Run"  -->  "Clean up build"

So you will get a clear "arm32"  ...

Then  select others builds (chipset)

Lazarus menu "Project" --> "Projec Options" --> "[LAMW] ...." ---> "Build"

"Run"  -->  "Clean up build"

Then  "Run"  --> "[LAMW] Build Android Apk and Run"
Title: Re: jOpenDialog crashes in Android 10
Post by: neuro on July 26, 2021, 05:50:11 am
I have more than 20 years of programming experience.
“Clean up build” was the first thing which I already tried in order to fix the problem before writing to this forum.
“Clean up build” does not help. File permissions do not help.
LAMW has a bug which needs to be fixed – jOpenDialog crashes on aarch64 Android 10 device.

Code: Pascal  [Select][+][-]
  1. listFile:= Self.GetFileList(Self.GetEnvironmentDirectoryPath(dirDownloads));
also crashes on aarch64 Android 10 device.
Title: Re: jOpenDialog crashes in Android 10
Post by: jmpessoa on July 26, 2021, 06:52:29 am

Quote
I have more than 20 years of programming experience.

Good!

Can you, please,  put here any reference appointed by run time log error ?
Title: Re: jOpenDialog crashes in Android 10
Post by: neuro on July 26, 2021, 07:48:22 am
07-26 08:43:20.371 22961 22961 E AndroidRuntime: FATAL EXCEPTION: main
07-26 08:43:20.371 22961 22961 E AndroidRuntime: Process: com.example.appopenfiledialogdemo1, PID: 22961
07-26 08:43:20.371 22961 22961 E AndroidRuntime: java.lang.NullPointerException: Attempt to get length of null array
07-26 08:43:20.371 22961 22961 E AndroidRuntime:        at com.example.appopenfiledialogdemo1.jOpenDialog.refresh(jOpenDialog.java:193)
07-26 08:43:20.371 22961 22961 E AndroidRuntime:        at com.example.appopenfiledialogdemo1.jOpenDialog.access$300(jOpenDialog.java:26)
07-26 08:43:20.371 22961 22961 E AndroidRuntime:        at com.example.appopenfiledialogdemo1.jOpenDialog$1.onItemClick(jOpenDialog.java:64)
07-26 08:43:20.371 22961 22961 E AndroidRuntime:        at android.widget.AdapterView.performItemClick(AdapterView.java:374)
07-26 08:43:20.371 22961 22961 E AndroidRuntime:        at android.widget.AbsListView.performItemClick(AbsListView.java:1736)
07-26 08:43:20.371 22961 22961 E AndroidRuntime:        at android.widget.AbsListView$PerformClick.run(AbsListView.java:4207)
07-26 08:43:20.371 22961 22961 E AndroidRuntime:        at android.widget.AbsListView$7.run(AbsListView.java:6692)
07-26 08:43:20.371 22961 22961 E AndroidRuntime:        at android.os.Handler.handleCallback(Handler.java:883)
07-26 08:43:20.371 22961 22961 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:100)
07-26 08:43:20.371 22961 22961 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:237)
07-26 08:43:20.371 22961 22961 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:8167)
07-26 08:43:20.371 22961 22961 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
07-26 08:43:20.371 22961 22961 E AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
07-26 08:43:20.371 22961 22961 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
Title: Re: jOpenDialog crashes in Android 10
Post by: jmpessoa on July 26, 2021, 07:21:49 pm

Quote
07-26 08:43:20.371 22961 22961 E AndroidRuntime: java.lang.NullPointerException: Attempt to get length of null array
07-26 08:43:20.371 22961 22961 E AndroidRuntime:        at com.example.appopenfiledialogdemo1.jOpenDialog.refresh(jOpenDialog.java:193)

Yes,  looks like the code don't handle an empty directory..... 

Are you testing on an empty directory?
Title: Re: jOpenDialog crashes in Android 10
Post by: neuro on July 26, 2021, 08:16:55 pm
Quote
Are you testing on an empty directory?
No, the directory is not empty.
Your questions make the impression that you don’t have aarch64 Android 10 device for code testing.
Please write your list of Android devices which you have at your disposal for code testing.
Title: Re: jOpenDialog crashes in Android 10
Post by: jmpessoa on July 26, 2021, 08:42:24 pm
 
Quote
Please write your list of Android devices which you have at your disposal for code testing.

Yes, I don't have Android 10 aarch64  ;D

My device list:  4.3 arm32,  5.01 arm32, 6.01 arm32,  7 arm32 and  9 aarch64
Title: Re: jOpenDialog crashes in Android 10
Post by: neuro on July 26, 2021, 09:53:24 pm
Please test “AppOpenFileDialogDemo1” on 9 aarch64 and write us the results.
Title: Re: jOpenDialog crashes in Android 10
Post by: jmpessoa on July 27, 2021, 02:47:20 am

Quote
Please test “AppOpenFileDialogDemo1” on 9 aarch64 and write us the results.....

Yes, it works!

So what I can do now is  improve de code to try fix this error:

Quote
07-26 08:43:20.371 22961 22961 E AndroidRuntime: java.lang.NullPointerException: Attempt to get length of null array
07-26 08:43:20.371 22961 22961 E AndroidRuntime: at com.example.appopenfiledialogdemo1.jOpenDialog.refresh(jOpenDialog.java:193)

You can get a modified (robust/defensive)  "jOpenDialog.java" code in the attachment...
[put it in LAWM folder "...\android_wizard\smartdesigner\java"]




Title: Re: jOpenDialog crashes in Android 10
Post by: neuro on July 27, 2021, 03:36:58 am
Your updated “jOpenDialog.java” now does not crash on Android 10 when “jOpenDialog1.Show()” button is clicked,
however the problem still remains because the returned file list is empty,
jOpenDialog does not see any files or directories.
It still fails to work in Android 10. Still needs to be fixed further.
Title: Re: jOpenDialog crashes in Android 10
Post by: neuro on July 27, 2021, 03:54:27 am
I guess this the same problem, and solution might be the following:
Quote
https://stackoverflow.com/questions/63089866/file-listfiles-doesnt-list-files-on-some-specific-paths
File.listFiles() doesn't list files on some specific paths
<....>
Try getFilesDir(), getExternalFilesDir(null) and getExternalFilesDirs(). If you add legacyExternalStorage true to manifest file you can also use getExternalStorageDirectory(). – blackapps Jul 25 '20 at 15:22

AFAIR apps don't have the permission to list files in /storage/emulated. Hence no matter what you do you won't get a file listing for this directory. Additionally I am sorry but what you do is a wast of time because with Android Scoped Storage you can no longer access /storage/emulated directly. Therefore your code will only work on older devices. – Robert Jul 25 '20 at 16:20

Quote
https://developer.android.com/about/versions/11/privacy/storage
Storage updates in Android 11
Android 11 (API level 30) further enhances the platform, giving better protection to app and user data on external storage. This release introduces several enhancements, such as raw file path access, batch edit operations for media, and an updated UI for the Storage Access Framework.

The release also offers improvements to scoped storage, which makes it easier for developers to fulfill their storage use cases after they migrate to using this storage model.

Scoped storage enforcement
Apps that run on Android 11 but target Android 10 (API level 29) can still request the requestLegacyExternalStorage attribute. This flag allows apps to temporarily opt out of the changes associated with scoped storage, such as granting access to different directories and different types of media files. After you update your app to target Android 11, the system ignores the requestLegacyExternalStorage flag.

Maintain compatibility with Android 10
If your app opts out of scoped storage when running on Android 10 devices, it's recommended that you continue to set requestLegacyExternalStorage to true in your app's manifest file. That way, your app can continue to behave as expected on devices that run Android 10.


Quote
https://developer.android.com/reference/android/os/Environment#getExternalStorageDirectory()
getExternalStorageDirectory
Added in API level 1
Deprecated in API level 29

public static File getExternalStorageDirectory ()
This method was deprecated in API level 29.
Alternatives such as Context#getExternalFilesDir(String), MediaStore, or Intent#ACTION_OPEN_DOCUMENT offer better performance.


<...>
getExternalStoragePublicDirectory
Added in API level 8
Deprecated in API level 29

public static File getExternalStoragePublicDirectory (String type)
This method was deprecated in API level 29.
Alternatives such as Context#getExternalFilesDir(String), MediaStore, or Intent#ACTION_OPEN_DOCUMENT offer better performance.


jmpessoa, please update “jOpenDialog.java” using information above.
Title: Re: jOpenDialog crashes in Android 10
Post by: jmpessoa on July 27, 2021, 04:48:34 am

Quote
Maintain compatibility with Android 10
If your app opts out of scoped storage when running on Android 10 devices, it's recommended that you continue to set requestLegacyExternalStorage to true in your app's manifest file. That way, your app can continue to behave as expected on devices that run Android 10.

Yes,  this solution is already adopted in "New Project".... you can test “jOpenDialog.java” creating a new project!

[I will update the manifest in "AppOpenFileDialogDemo1" and others old "demos"]

Thank you!
Title: Re: jOpenDialog crashes in Android 10
Post by: neuro on July 27, 2021, 01:43:31 pm
jmpessoa, this is incorrect way to solve the problem, your fix in incorrect, because requestLegacyExternalStorage does not work in targetSdkVersion 30.
In LAMW almost half of components (which use access to files) do not work in targetSdkVersion 30 due to this problem, so this is very important and serious issue.
You need to use another solution of the problem, please see below.

Quote
https://newbedev.com/requestlegacyexternalstorage-is-not-working-in-android-11-api-30
requestLegacyExternalStorage is not working in Android 11 - API 30
Solution:
But now when I targetSdkVersion 30, this no longer seems to work

That is correct. Android 11 (API 30+) requestLegacyExternalStorage=true does nothing and you can no longer "opt-out". It was available in Android 10 to give developers a trasition/grace period to be able to migrate to the scoped storage model.

Quote
https://stackoverflow.com/questions/65177387/why-androidrequestlegacyexternalstorage-true-not-working-in-android-10-api
Why android:requestLegacyExternalStorage=“true” not working in Android 10 - API 29
<…>
I also found another (probably better) solution. Instead of using external public storage, I switched to external storage of the app:

downloadRequest.setDestinationInExternalFilesDir(...)
and to access the file use

new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), mFileName);

instead of now deprecated Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)

As a bonus there's no need to use android:requestLegacyExternalStorage="true" in AndroidManifest.

jmpessoa, please update all LAMW components using information above.
Title: Re: jOpenDialog crashes in Android 10
Post by: jmpessoa on July 27, 2021, 04:51:13 pm
Quote
please update all LAMW components using information above.

Ok. I will try!

Thank you!
Title: Re: jOpenDialog crashes in Android 10
Post by: jmpessoa on July 27, 2021, 06:24:54 pm
Please, Can you test this first [draft] fix in attachment?

Put it in LAWM folder "....\android_wizard\smartdesigner\java"
Title: Re: jOpenDialog crashes in Android 10
Post by: neuro on July 27, 2021, 08:04:18 pm
Your “first [draft] fix” does not work in Android 10. The returned file list is empty, it does not see files.
When I set “jOpenDialog1.InitialDirectory” property to “dirPictures” it just shows “Pictures” directory and no files, no subdirectories.
When I set “jOpenDialog1.InitialDirectory” property to “dirDCIM” it just shows “DCIM” directory and no files, no subdirectories.
When I set “jOpenDialog1.InitialDirectory” property to “dirDownloads” it just shows “Downloads” directory and no files, no subdirectories.

This is serious problem, because in LAMW almost half of components (which use access to files) do not work in targetSdkVersion 30 due to this problem. This is not the problem of single “jOpenDialog” component, it is the problem of all LAMW components.
Title: Re: jOpenDialog crashes in Android 10
Post by: jmpessoa on July 27, 2021, 08:13:03 pm
Quote
This is not the problem of single “jOpenDialog” component, it is the problem of all LAMW components.

Yes, but if we fix “jOpenDialog” the we can fix all others......

following your tip from here:
Quote
I also found another (probably better) solution. Instead of using external public storage, I switched to external storage of the app:

downloadRequest.setDestinationInExternalFilesDir(...)
and to access the file use

new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), mFileName);

instead of now deprecated Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)

Here I am trying  a code like this:

Code: Pascal  [Select][+][-]
  1. private File getMyEnvDir(String environmentDir) {
  2.        if (Build.VERSION.SDK_INT <  29) {
  3.            return Environment.getExternalStoragePublicDirectory(environmentDir);
  4.        }
  5.        else {
  6.            return controls.activity.getExternalFilesDir(environmentDir);
  7.        }
  8.    }
  9.  

in the expectation that they return the [same] correct path .....
Title: Re: jOpenDialog crashes in Android 10
Post by: neuro on July 27, 2021, 08:25:14 pm
It looks that we have encountered bigger problem than we initially thought.
It looks like Google is forcing us to make sandboxed apps which are unable to access files. Google named this limitation as “Scoped Storage in Android”. We need to search for articles which contain keyword “Scoped Storage”, as for example:
 
Quote
https://www.androidcentral.com/what-scoped-storage
What is Scoped Storage in Android?
A controversial change that originally was slated for Android 10 becomes mandatory for all new apps in August 2020 and every app targeting Android 11.
Jerry Hildenbrand
17 Aug 2020
When Android 10 was still in its early beta days, Google had plans for a big change in the way that apps could access the files and folders in your phone's external storage (things like photos that you or other apps placed in the /data/media folder, not the SD card present in many Android models).
This change was known as Scoped Storage, and its purpose was to kill the abuse of the READ_EXTERNAL_STORAGE permissions that so many apps and developers like to request.
These changes were originally slated to apply to every app on a phone running Android 10 or later, but because of developer backlash Google changed course and only required the use of Scoped Storage for apps that target Android API level 29, which is Android 10. But with Android 11 Scoped Storage is back, and Google isn't likely to change its mind this time. <...>

Quote
https://developer.android.com/training/data-storage
Scoped storage
To give users more control over their files and to limit file clutter, apps that target Android 10 (API level 29) and higher are given scoped access into external storage, or scoped storage, by default. Such apps have access only to the app-specific directory on external storage, as well as specific types of media that the app has created.
Note: If your app requests a storage-related permission at runtime, the user-facing dialog indicates that your app is requesting broad access to external storage, even when scoped storage is enabled.
Use scoped storage unless your app needs access to a file that's stored outside of an app-specific directory and outside of a directory that the MediaStore APIs can access. If you store app-specific files on external storage, you can make it easier to adopt scoped storage by placing these files in an app-specific directory on external storage. That way, your app maintains access to these files when scoped storage is enabled.
To prepare your app for scoped storage, view the storage use cases and best practices guide. If your app has another use case that isn't covered by scoped storage, file a feature request. You can temporarily opt-out of using scoped storage.
Title: Re: jOpenDialog crashes in Android 10
Post by: neuro on July 28, 2021, 03:27:15 am
Quote
https://betterprogramming.pub/android-scoped-storage-demystified-3024a062ba24
With Android 11, Some major changes and restrictions are added to enhance user privacy, including, as listed in preview to behavior changes, the following:
Scoped storage enforcement: Access into external storage directories is limited to an app-specific directory and specific types of media that the app has created.
Permissions auto-reset: If users haven’t interacted with an app for a few months, the system auto-resets the app’s sensitive permissions.

Background location access: Users must be directed to system settings in order to grant the background location permission to apps.
Package visibility: When an app queries for the list of installed apps on the device, the returned list is filtered.

Quote
https://www.raywenderlich.com/10768834-scoped-storage-tutorial-for-android-11-deep-dive
Introducing Scoped Storage
Scoped storage brings two major changes. First, you no longer have access to a file via its path. Instead, you need to use its Uri. Second, if you want to modify a file not created by your app, you need to ask the user for permission.
There are two possibilities for doing this.
One is via the MediaStore API that allows you to query the device for images, videos and audio.
The other is using the Storage Access Framework (SAF), which opens the native file explorer and allows you to request access either for a specific file or for its root folder — depending on whether you’re using the action ACTION_OPEN_DOCUMENT or ACTION_OPEN_DOCUMENT_TREE, respectively.
<…>
Bulk Operations
One of the limitations on Android 10 is the lack of support for bulk operations performed on files. Every time you want to do something to multiple files, you need to iterate over a list and ask the user for consent for each individual file.
Android 11 added two new methods to solve this:
createWriteRequest
createDeleteRequest
Both methods support modifying multiple files at the same time with a single request.

jmpessoa, file access is more complicated than we initially thought. Which way we should go to solve this, what do you think?
Title: Re: jOpenDialog crashes in Android 10
Post by: jmpessoa on July 28, 2021, 04:52:07 am
Quote
Which way we should go to solve this, what do you think?

It won't be easy for anyone... :D :o

But, after the last [LAMW] update I think we have a consistent behavior ...

All we need for now is to inform developers that for Android 11

the trivail/popular/extern folders  (Picture, Download, .....) are no longer accessible and are replaced for

homonymous/namesakes in the application space ..... which are initially empty!

//This is the legacy solution that preserve the old/deprecated behavior for  Android <= 10
Code: Pascal  [Select][+][-]
  1.    private File getMyEnvDir(String environmentDir) {
  2.        if (Build.VERSION.SDK_INT <= 29) {      //  <<----legacy
  3.            return Environment.getExternalStoragePublicDirectory(environmentDir);
  4.        }
  5.        else {
  6.            return controls.activity.getExternalFilesDir(environmentDir);
  7.        }
  8.    }
  9.  

I do some test here and it works! (You can try, again,  AppOpenFileDialogDemo1)

but:
 
1) First click the button  "Get File List"  to put/write some files in directory   [if you want to test "Picture" folder change the code ....]
2) Click the button  "jOpenDialog1.Show()"  and the pop-up will show the file names write in sep (1)
Title: Re: jOpenDialog crashes in Android 10
Post by: neuro on July 28, 2021, 07:13:20 am
jmpessoa, thank you for the update. Now “AppOpenFileDialogDemo1” works and shows files and subdirectories in Android 10.
Important notice for other readers: you need to update LAMW, otherwise “AppOpenFileDialogDemo1” will fail to compile resulting in multiple compilation results.
jmpessoa, have you already updated other LAMW components which use file access?
Title: Re: jOpenDialog crashes in Android 10
Post by: jmpessoa on July 28, 2021, 07:28:12 am
Quote
have you already updated other LAMW components which use file access?

Yes!  (maybe I forgot some but i'll keep looking...)

Thank you!

Title: Re: jOpenDialog crashes in Android 10
Post by: neuro on July 29, 2021, 05:59:31 am
I have updated LAMW to the latest version.
jEditText function “LoadFromFile” does not work in aarch64 Android 10.

Code: Pascal  [Select][+][-]
  1. fails to work --> jEditText1.LoadFromFile(filename);
  2. works successfully --> jEditText1.Text := jTextFileManager1.LoadFromFile(ExtractFileDir(filename), ExtractFileName(filename));

jmpessoa, please fix function “LoadFromFile”.
Title: Re: jOpenDialog crashes in Android 10
Post by: jmpessoa on July 29, 2021, 07:52:31 am

Fixed  "jEditText.java" and "jTextFileManager.java"  in github!

But, you can get a fixed copy from  attachments ...

[put its in LAMW folder "....\android_wizard\smartdesigner\java"]

Thank you!
Title: Re: jOpenDialog crashes in Android 10
Post by: neuro on July 29, 2021, 09:18:26 am
Thank you, your fixed  "jEditText.java" and "jTextFileManager.java" now work in aarch64 Android 10.
Title: Re: jOpenDialog crashes in Android 10
Post by: neuro on March 10, 2022, 03:27:03 am
Code: Pascal  [Select][+][-]
  1. jBitmap.LoadFromFile
does not work on Android 10.

On Android 10 (64-bit, device: Samsung Galaxy S9)
both
Code: Pascal  [Select][+][-]
  1. ImageView1.LoadFromURL('file://' + bmp_image_filename);
and
Code: Pascal  [Select][+][-]
  1. ImageView1.SetImage(jImageFileManager1.LoadFromUri('file://' + bmp_image_filename));
work successfully and load bmp image.

However both
Code: Pascal  [Select][+][-]
  1. jBitmap1.GetImageFromFile(bmp_image_filename);
and
Code: Pascal  [Select][+][-]
  1. jBitmap1.LoadFromFile(bmp_image_filename);
fail to load the bmp image.

Sample demo project is attached.
jmpessoa, please fix jBitmap.LoadFromFile.
Title: Re: jOpenDialog crashes in Android 10
Post by: jmpessoa on March 10, 2022, 06:03:18 pm
@neuro
Code: Pascal  [Select][+][-]
  1. mpessoa, please fix jBitmap.LoadFromFile.
  2.  


Ok. I will try....

but Android 10 now has a new file access model/paradigm : the demo is here  "AppPublicFoldersAccessDemo1"

What about?
Title: Re: jOpenDialog crashes in Android 10
Post by: neuro on March 10, 2022, 07:40:14 pm
Android 10 now has a new file access model/paradigm : the demo is here  "AppPublicFoldersAccessDemo1"
Quote
https://developer.android.com/training/data-storage/shared/documents-files#java
https://petrakeas.medium.com/android-10-11-storage-cheat-sheet-76866a989df4
 Android 11 - Api 30  has restricted developers to access devices folders (ex: "Download, Documents, ..., etc")
 Developers no longer have access to a file via its path. Instead, you need to use via “Uri“.
This means we need to add new function
jBitmap.LoadFromUri
or
jBitmap.LoadFromURL
because jBitmap.LoadFromFile is no longer available in Android 11.

Btw, on Android 8.1.0 jBitmap.LoadFromFile fails work too.
Both fail to work on Android 8.1.0:
Code: Pascal  [Select][+][-]
  1. jBitmap1.GetImageFromFile(bmp_image_filename);
and
Code: Pascal  [Select][+][-]
  1. jBitmap1.LoadFromFile(bmp_image_filename);
Title: Re: jOpenDialog crashes in Android 10
Post by: Insid3Code on March 11, 2022, 12:07:59 am
Try like this:
Code: Pascal  [Select][+][-]
  1.    jBitmap1.LoadFromFile('/Download/image01.bmp');
  2.    ImageView1.SetImage(jBitmap1.GetImage()
  3.  
After changing jBitmap1 FilePath property to fpathExt

See Animated gif:
(https://i.imgur.com/8QMCBtE.gif)
Tested on Emulator (Android 7 and 11)

Title: Re: jOpenDialog crashes in Android 10
Post by: neuro on March 11, 2022, 01:06:28 am
Insid3Code, changing jBitmap1 FilePath property to fpathExt has solved the problem – now jBitmap.LoadFromFile works.

However another function
jBitmap1.GetImageFromFile(bmp_image_filename);
still does not work, changing jBitmap1 FilePath property to fpathExt does not help for jBitmap.GetImageFromFile.
Title: Re: jOpenDialog crashes in Android 10
Post by: Insid3Code on March 11, 2022, 03:05:49 pm
because GetImageFromFile doesn't need FilePath property...

the right way is:
Code: Pascal  [Select][+][-]
  1. ImageView1.SetImage(jBitmap1.GetImageFromFile(bmp_image_filename));
Tested on Emulator (Android 7 and 11)
Title: Re: jOpenDialog crashes in Android 10
Post by: neuro on March 26, 2022, 02:17:53 am
jmpessoa has fixed jOpenDialog and now jOpenDialog works in Android 10.
However in Android 12 jOpenDialog fails to work – it returns empty list of files – it cannot see any files.
You can check that out using Android Studio’s Virtual Device Manager – for example, you can create virtual “Pixel 4” phone which runs API 32 (Android 12).

Android 10 introduced new storage paradigm for apps called scoped storage. Scoped storage changes the way apps store and access files on a device's external storage.
Since Android 10 apps can no longer read files using file path, now you have to use Uri for file reading.
In every new Android version release Google makes it harder to access files.
In Android 12 you get only the filename from Uri, and you have no way to get real path of the file.
jOpenDialog can no longer work under such restrictions.
It looks like jOpenDialog component has reached dead-end and should be removed from LAMW and should be replaced by system's file picker app.
Quote
https://developer.android.com/training/data-storage/shared/documents-files
In these cases, allow the user to choose the file to open by invoking the ACTION_OPEN_DOCUMENT intent, which opens the system's file picker app.

LAMW provides demo how to use system's file picker app, which is located in folder “AppPublicFoldersAccessDemo1”.
I have added essential functions which enable to use system's file picker app (I have made "pull requests" for changes in github):
Code: Pascal  [Select][+][-]
  1. function LoadBytesFromUri(_treeUri: jObject): TDynArrayOfJByte;
  2. procedure SaveBytesToUri(_bytes: TDynArrayOfJByte; _toTreeUri: jObject);
  3. function GetFileNameByUri(_treeUri: jObject): string;

Now everybody will have the ability to load and save bytes to Uri using system's file picker app, and now jOpenDialog is no longer needed, and jOpenDialog can be safely removed from LAMW.
A bit later I will make “AppPublicFoldersAccessDemo2” which will show how to use system's file picker app for loading and saving files.
Title: Re: jOpenDialog crashes in Android 10
Post by: neuro on March 26, 2022, 06:20:55 pm
I have created “AppPublicFoldersAccessDemo2”, which shows how to use system's file picker app for loading and saving files (bytes, images, and text files). Zip’ed file is attached.
jmpessoa,please add “AppPublicFoldersAccessDemo2” to LAMW “lazandroidmodulewizard/demos/GUI/” directory, because I don’t know how to upload directory to LAMW github.
TinyPortal © 2005-2018