Recent

Author Topic: jOpenDialog crashes in Android 10  (Read 13942 times)

neuro

  • New Member
  • *
  • Posts: 26
Re: jOpenDialog crashes in Android 10
« Reply #15 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.
« Last Edit: July 27, 2021, 04:19:53 am by neuro »

jmpessoa

  • Hero Member
  • *****
  • Posts: 1915
Re: jOpenDialog crashes in Android 10
« Reply #16 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!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

neuro

  • New Member
  • *
  • Posts: 26
Re: jOpenDialog crashes in Android 10
« Reply #17 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.

jmpessoa

  • Hero Member
  • *****
  • Posts: 1915
Re: jOpenDialog crashes in Android 10
« Reply #18 on: July 27, 2021, 04:51:13 pm »
Quote
please update all LAMW components using information above.

Ok. I will try!

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

jmpessoa

  • Hero Member
  • *****
  • Posts: 1915
Re: jOpenDialog crashes in Android 10
« Reply #19 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"
« Last Edit: July 27, 2021, 06:28:33 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

neuro

  • New Member
  • *
  • Posts: 26
Re: jOpenDialog crashes in Android 10
« Reply #20 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.

jmpessoa

  • Hero Member
  • *****
  • Posts: 1915
Re: jOpenDialog crashes in Android 10
« Reply #21 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 .....
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

neuro

  • New Member
  • *
  • Posts: 26
Re: jOpenDialog crashes in Android 10
« Reply #22 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.
« Last Edit: July 27, 2021, 08:34:04 pm by neuro »

neuro

  • New Member
  • *
  • Posts: 26
Re: jOpenDialog crashes in Android 10
« Reply #23 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?

jmpessoa

  • Hero Member
  • *****
  • Posts: 1915
Re: jOpenDialog crashes in Android 10
« Reply #24 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)
« Last Edit: July 28, 2021, 04:56:05 am by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

neuro

  • New Member
  • *
  • Posts: 26
Re: jOpenDialog crashes in Android 10
« Reply #25 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?
« Last Edit: July 29, 2021, 06:02:57 am by neuro »

jmpessoa

  • Hero Member
  • *****
  • Posts: 1915
Re: jOpenDialog crashes in Android 10
« Reply #26 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!

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

neuro

  • New Member
  • *
  • Posts: 26
Re: jOpenDialog crashes in Android 10
« Reply #27 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”.

jmpessoa

  • Hero Member
  • *****
  • Posts: 1915
Re: jOpenDialog crashes in Android 10
« Reply #28 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!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

neuro

  • New Member
  • *
  • Posts: 26
Re: jOpenDialog crashes in Android 10
« Reply #29 on: July 29, 2021, 09:18:26 am »
Thank you, your fixed  "jEditText.java" and "jTextFileManager.java" now work in aarch64 Android 10.

 

TinyPortal © 2005-2018