alexrintt/shared-storage

No Read Permission on PersistedUris

ankitparmar007 opened this issue ยท 26 comments

final selectedUriDir = await openDocumentTree();

selectedUriDir = content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fmedia%2Fcom.whatsapp%2FWhatsApp%2FMedia%2F.Statuses

final listOfPersistedUris = await persistedUriPermissions();

listOfPersistedUris [UriPermission(isReadPermission: false, isWritePermission: true, persistedTime: 1638318739830, uri: content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fmedia%2Fcom.whatsapp%2FWhatsApp%2FMedia%2F.Statuses)]

AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

**

  • compileSdkVersion 31
  • minSdkVersion 24
  • targetSdkVersion 31

**

Thanks for the issue, what kind of error are you having?

If I understood correctly, you don't need the read permission because currently the package always grants write permission

If you have the write permission we don't need the read permission

how can I read all the data from below given specific folder URI ?

Ex. content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fmedia%2Fcom.whatsapp%2FWhatsApp%2FMedia%2F.Statuses

Unfortunately this usecase is not currently supported, but I added this as a new feature request in #11, I'm planning to implement it on this weekend. Feel free to implement it by yourself if you want! Thanks for the issue.

Thank you so much for your hard-work, But I don't know much about Java so I will use this package once you add support for read data.

Additional note
Please note that once we get the Persisted-Uri, Package should read all the files from the specific folder without user interaction.
So we can show all the data in App UI every time whenever user open up the app.

Once again Thank you so much for this amazing package :)

Hey @ankitparmar007! g'afternoon

Your usecase is to get large data sets? Like a folder with 1000+ files?

Hey @LaksCastro

Glad to know that you are putting so much effort on this package :) Thank you so much for that !

Based on this URI

content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fmedia%2Fcom.whatsapp%2FWhatsApp%2FMedia%2F.Statuses

I am trying to read WhatsApp Statuses folder from this path => Android/media/com.whatsapp/WhatsApp/Media/.Statuses

So there is a possibility if user have business account or large user base in WhatsApp account then, Statuses may be available up-to 500 or more including images and videos.

As I can see that in 11 you have mentioned that you are facing issue for reading files which folder has so many files in it.

In my opinion you can limit the number of files reads up-to 1000 if you have no any other solution available currently.

Addition Note
I forget to mentioned in my last issues that openDocumentTree() will open the root directory and user have to manually navigate to this PATH like Android/media/com.whatsapp/WhatsApp/Media/.Statuses, would be appreciated if you add a property like this openDocumentTree(Path = "Android/media/com.whatsapp/WhatsApp/Media/.Statuses") so we can change Path parameter for easy navigation :)

If you need any other Help, For testing the package or live help on social media where we can easily communicate, I would really like to connect with you on you preferred social media account :)

Hey! Thanks for sharing your usecases.

Okay, I think I got your needs, this is about WhatsApp statuses and you need the initialDir. Is really useful to know what you need, to keep the focus on the priorities. Thanks for that.

Also, I think you'll need an way to export these files to another location, so maybe I also need to include some copy API in this next release.

  • So I'll finish the "database query" (Fix large data set issue)
  • Add initialDir param
  • Add copyTo API to allow you export the statuses

Btw, my Discord is always open laks#1712, thanks for using the package :)

Ya I think You need to copy or cached all the files from URI specified folder to application cached folder for easy read I guess,
Also I have sent a friend request on Discord for effective communication.

Hardcoded WhatsApp Status Media URI to use as initialUri when prompting the user

content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fmedia/document/primary%3AAndroid%2Fmedia%2Fcom.whatsapp%2FWhatsApp%2FMedia%2F.Statuses

Usage:

const kWhatsAppStatusUri = '...';

/// Prompt user with a folder picker (Available for Android 5.0+)
await openDocumentTree(initialUri: Uri.parse(kWhatsAppStatusUri));

@lakscastro Good to see you working on package again :)
I have a quick tip for you that might help for caching data into temp directory of the user App using this open source package code https://pub.dev/packages/path_provider

Syntax to get application specific temp directory using path provider package
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;

Maybe You can store all the data given by openDocumentTree() method and store it in application specific cache folder(path given by tempPath) so app owner can easily clean up temp folder for storing more fresh data or can be deleted easily when there is no need of it.

Thank you so much again :)

yeah, of course, you can store the path wherever you want. You can also use a package like shared_preferences to store de data. Thanksful for the tip! =]

I mean that this specific path of the WhatsApp status is not available to get it from anywhere, so we need to hardcode it to use as initialDir

But be aware that this is the solution that I'm trying, you can send me any solutions that you think that will fill better our usecase!

@lakscastro
I think shared_preferences cannot store images and videos, and we need to cache images and videos for instance, then application specific temp directory only be helpful for storing temporary data, in my opinion

Also end user won't pass any path to initialDir parameter, Only app developer can assign path to initialDir to open a specific folder, so in my opinion you don't need to hard-code WhatsApp Statuses Path.

If app Developer don't want to assign anything to initialDir then root directory will be open otherwise assigned path will be open using openDocumentTree().

If you didn't get it, no problem just let me know what you didn't understand, I will share video example on discord :)

where do you'll get the assigned path?

@lakscastro
/// Get permissions to manage an Android directory using shared_storage package
final selectedUriDir = await openDocumentTree();

print(selectedUriDir);

currently we can get WhatsApp Statues folder path by first opening root directory using openDocumentTree(); method and later user has to travel to this path /storage/emulated/0/Android/Media/com.whatsapp/WhatsApp/Media/.Statuses/

later openDocumentTree() method return the path for user selected folder and as you mentioned in above comment we can store same path in shared_preferences so user doesn't have to travel all the time and we can use same path for future use that's a good thing :)

But my point is can we read all the files from this path /storage/emulated/0/Android/Media/com.whatsapp/WhatsApp/Media/.Statuses/ without user interaction after we get the read permission from the user for the first time ?

below is an example for how we can read files

@override
void initState() {
  super.initState();
  var data = await openDocumentTree(initialUri: Uri.parse("content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fmedia/document/primary%3AAndroid%2Fmedia%2Fcom.whatsapp%2FWhatsApp%2FMedia%2F.Statuses%2FMedia"));
}

later we can show data in APP UI ???

also when calling openDocumentTree() method for the first time we can set initialDir path like this
final selectedUriDir = await openDocumentTree(initialDir="/storage/emulated/0/Android/Media/com.whatsapp/WhatsApp/Media/.Statuses/");

consider a point in this case we know the path already so we can directly assign path to initialDir parameter so user doesn't have to travel for the first time also and user can give the permission to read all the data from specified path :)

Hope you get my point :)

currently, path/to/something/will/not/work due the SAF API, they removed completely the File based API to work with files. Now we need to use the DocumentFile based API that is based on URIs

Ohh that's a bad news :(
So you mean we can't use File App to get the user specified forlder which is inbuilt in almost every android phone ?

No, I mean that we just need to use SAF (Android 5.0+)

Most File APIs are now available in the DocumentFile API

Starting from new Android APIs (API Level 30+) File based APIs are not available anymore

Bro Lets discuss on discord for effective and fast communication :)

Oh, of course

@lakscastro hey bro
I think I have found a solution !
https://www.youtube.com/watch?v=1evcTdwZxwM
As show in this video we can read all files directly using uri for example

image uri would be like this
content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fmedia/document/primary%3AAndroid%2Fmedia%2Fcom.whatsapp%2FWhatsApp%2FMedia%2F.Statuses%2FMedia%2Fimage1.jpg

Decoded image uri looks like this
content://com.android.externalstorage.documents/tree/primary:Android/media/document/primary:Android/media/com.whatsapp/WhatsApp/Media/.Statuses/Media/image1.jpg

or for video uri looks like this
content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fmedia/document/primary%3AAndroid%2Fmedia%2Fcom.whatsapp%2FWhatsApp%2FMedia%2F.Statuses%2FMedia%2Fvideo1.jpg

so can you try like this once we select the directory using openDocumentTree() then this method should return LIST OF URIs for all the files located in selected folder

@ankitparmar007 @lakscastro Hello folks. Does this package support the reading files from the Android folder using SAF DocumentTree yet? :)

@jvoltci not fully ! This package needs more improvements

Hey @ankitparmar007 ! Let's evolve this package because I researched and could not find any relevant package for this problem. Let's catch up, ping me on @jvoltci tweeter if are up!

Hey @jvoltci Sorry to say but, I am not a twitter user ! you can connect with me on discord using this username ankit007#2133

Closing this issue as resolved since all mentioned use-cases are now available in v0.4.x.

Please, you can always re-open this one or open a new issue. Any other bug-reports, questions or feature requests are always welcome!

Thanks for taking time to contribute to this package.