PathFromUri function works on AI Companion, But not when building .apk

@Floof_h this works for media files.

Yes, but also for all files (non-media files).
For media files you only need READ permission to access those files.
But for non-media files (like .txt or .csv), that are not created by the app you need MANAGE_EXTERNAL_FILES to access those files if they are stored in the external storage (outside of the ASD).

But I am getting same error after selecting pdf and txt files.
Image files are working.

For image (media) files you only need READ permission.

How was this file saved there? Who / which app created this file?
The app can only access it if the csv (non-media) file is created by the app itself.

this not really is a solution, because usually apps, which use that permission are rejected by Google, see here, there are a few exceptions...

we still want to be able to access files, which have been provided by other apps in the shared directories... it looks like it is not possible anymore in App Inventor now starting with the new SDK30 release? However theoretically it is possible to still access these files.... see here Cómo abrir archivos con el framework de acceso al almacenamiento  |  Desarrolladores de Android  |  Android Developers @ewpatton, do you plan to add this back into App Inventor?

When should you request the All files access permission?

You should only access the all files access permission when your app cannot effectively make use of the more privacy friendly best practices, such as using the Storage Access Framework or the Media Store API

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by icon24 Taifun.

2 Likes

Of course, and I didn't say that this is a solution.
I just wanted to show one way how it would work. It is clear that this is not an option for an app that is to be published in the Play Store.

Yes, it remains to be seen whether this will be implemented in AI2 ...

@Anke The file comes from the user. Basically, the file is a route to use for navigation.. the user creates his desired route (outside the app, using a website) and then downloads the route file to his phone. where the file ends up being, I don't know. I don't know the file's name either.. the app should be able to work with that.. (as any other normal app would). the user should be able to browse his phone's file (open the file manager from the app) and then select the file. then display the contents in a label, for starters..

@Taifun I did end up succeeding in reading files from the external storage, both on android 11, and on android 9, using your help and @TIMAI2 help from the previous thread I posted. However, this only worked on the companion... when I compiled the .apk, it stopped working, mainly because of what I mentioned in this thread (the "PathFromUri" function isn't working for android 11) but this function worked for android 9, though the "ReadFrom" function to read the file, was unsuccessful (on the apk).

Here's the situation again, for now:

  1. On the companion:
    I managed to read files from external storage, regardless of where/how the file was created. both on android 11 and on android 9 (so all SDK?). the whole app works as desired.
  2. Compiled .apk
    On android 11, the "PathFromUri" function isn't working, so I didn't even get to try to read the file because I can't even get it's path. so i need to fix this, before moving on to test the "ReadFrom" function to read the file..
    On android 9, the "PathFromUri" function works. however, I haven't succeeded yet to read the file because I believe the response from the "FileCopied" isn't successful. so there's a problem with the "CopyFileAsync" on my android 9..

Here are the blocks for the whole situation:

Thank you all for your help!

This Android 11 thing is quite confusing. :roll_eyes:

Can you show the blocks?
Maybe something is wrong with path.

I posted all of the blocks in the previous post. It should deal with both android 11 and 9 (SDK above 29 and below..)
But here it is again, with a few notes:

The path is correct on android 9. it's the CopyFileAsync that's not working as intended. Do you mean the path that's used as the source for the CopyFileAsync? this is the path that I get: image
On android 11, I cannot even get the path, using the "PathFromUri" function. it's returning nothing..

I asked for copy file blocks.Found it, sorry.
Also you should always rely on successful (Boolean) param.

Here they are:

I cannot control the param that's returned as a response I believe.. it's returned from the "FileCopied" function.. I tried it as a boolean first, but it returned an error, so i displayed it as a text and saw the value it returned (which is copy successful if the file was copied successfully).

I think I have found the problem.Lemme open my laptop.

No worries, take your time. thank you a lot for your help!

I have to say again though, that the app works when using the AI2 companion, but not when building the .apk

Thank you again..

Sorry, I thought of something else.

In that case, the problem can be because of READ_EXTERNAL_STORAGE permission.

I downloaded APK editor studio in order to view and edit the manifest, the READ_EXTERNAL_STORAGE permission and the MANAGE_EXTERNAL_FILES are both in there.
What do I have to do in order to fix my problem?

Thank you.

You also need to ask them using Screen1.AskPermission block.

Something like this?

Yes, that's right.

Alright, will try it out and then let you know.
Thank you so much for your help!