HBiSoft / PickiT

An Android library that returns real paths from Uri's
MIT License
292 stars 54 forks source link

Working with scoped storage #4

Closed droidluv closed 5 years ago

droidluv commented 5 years ago

This gem of a library is amazing in that it does what it advertises to do, but with scoped storgaes from Android 10 even with WRITE_EXTERNAL_STORAGE or the read permission File(filepath) won't be readable anymore, any good advices @HBiSoft?

droidluv commented 5 years ago

On a side note for Android 10 using android:requestLegacyExternalStorage="true" helps but its going to be strictly enforced from Android 11

HBiSoft commented 5 years ago

As it stands currently, the library will try to read the data column. If it fails, the file will be copied/written to a temporary folder and the path to that file will be returned.

From android 10 and onwards this will be the only way to get the "real" path of a file.

Have you tested the library on a device running Android 10?

droidluv commented 5 years ago

I did I tested on a Pixel 2 and I still keep getting the correct path, the problem lies in accessing the file, trying to read File(filepath) where filepath is returned by your library, throws an access denied error, basically making the filepath useless in a scoped situation. It's like a shiny candy which we can only look at.

On Wed, Oct 2, 2019, 8:53 PM Hagen Brooks notifications@github.com wrote:

As it stands currently, the library will try to read the data column. If it fails, the file will be copied/written to a temporary folder and the path to that file will be returned.

From android 10 and onwards this will be the only way to get the "real" path of a file.

Have you tested the library on a device running Android 10?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/HBiSoft/PickiT/issues/4?email_source=notifications&email_token=ACLQIBQ6NKNYIDUKWWNZAPLQMS4I7A5CNFSM4I4XPRU2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAFES4Q#issuecomment-537545074, or mute the thread https://github.com/notifications/unsubscribe-auth/ACLQIBQBDTWP4QIP5Y4RTBTQMS4I7ANCNFSM4I4XPRUQ .

HBiSoft commented 5 years ago

Wait, can you elaborate on what you said "trying to read File(filepath)"? Please provide your implementation and use case, including your Intent.

droidluv commented 5 years ago

Ok I wanted to retrieve all image and video type on a phone so I asked for read/write storage permissions and do this

              val intent = Intent(Intent.ACTION_GET_CONTENT)
                intent.type = "image/*"
                intent.putExtra("return-data", true)
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
                intent.putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/*", "video/*"))
                startActivityForResult(intent, FILE_REQUEST_CODE)

on Activity Result I call

        if (requestCode == FILE_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            pickiT?.getPath(data?.data, Build.VERSION.SDK_INT)
          }

in PickiTonCompleteListener I have

           val retrievedFile = File(path)
           if(wasSuccessful) Glide.with(this@Activity).load(retrievedFile).into(myImageView)

Here Glide will throw a FileNotFound exception with an access denied error in Android 10 if you don't have this android:requestLegacyExternalStorage="true", basically which means scoped storage restrictions come into place, from Android 11 android:requestLegacyExternalStorage="true" won't do anything, so its just a matter of time.

HBiSoft commented 5 years ago

It seems that the only reliable way going forward (Android 10 >) is to create a copy of the file in your application's private directory. Currently, this library will copy the file to your application's private directory if the file was selected from, for example, Dropbox.

Please test this by selecting an image/video from Dropbox or Google Drive. After the file has been copied, it will return a Uri of the copied file, this can be a file:// scheme since it is in your application's directory.

Once you have tested this, I will make the changes accordingly.

droidluv commented 5 years ago

@HBiSoft Yup file selection from Google Drive works it copies into the app directory, but damn it takes a long time, isn't this a negative experience?

HBiSoft commented 5 years ago

Well, you have 2 options.

The first is using input streams, this is how Google wants us to consume Uri's. The problem with this is that some libraries/API's requires you to pass a File object or a file:// Uri scheme. In this case, you will have to copy the file to your application's directory (this is where the second option comes in) and that is why this library was created in the first place.

HBiSoft commented 5 years ago

Closing since I can't do anything about this. I will put this in the read me.

HBiSoft commented 4 years ago

@droidluv I thought I would update you.

Android 11 (R), will have file paths enabled once again.. It is still unclear how this works, I tried testing it and could not get it working. I opened an issue on issuetracker.google.com and waiting for a response.

This will mean that you will keep android:requestLegacyExternalStorage="true", this will ensure that this library works on Android 10. I will update the library for Android 11 as soon as I get feedback from Google.


I will update the read me

droidluv commented 4 years ago

Thanks for keeping us posted, Android 11 is quite worrying me on how I can expect changes.

On Thu, Mar 19, 2020, 3:25 PM Hagen Brooks notifications@github.com wrote:

@droidluv https://github.com/droidluv I thought I would update you.

Android 11 (R), will have file paths enabled once again. https://developer.android.com/preview/privacy/storage#all-files-access. It is still unclear how this works, I tried testing it and could not get it working. I opened an issue on issuetracker.google.com and waiting for a response.

This will mean that you will keep android:requestLegacyExternalStorage="true", this will ensure that this library works on Android 10. I will update the library for Android 11 as soon as I get feedback from Google.

I will update the read me

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/HBiSoft/PickiT/issues/4#issuecomment-601088599, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACLQIBRESO3J7S4EZMFOWM3RIHTXNANCNFSM4I4XPRUQ .

HBiSoft commented 4 years ago

@droidluv No need to worry, Google always provides an alternative.

I did some tests on Android 11 and I was still able to access the data column. So this library will survive :)

The only thing you have to do is, add android:requestLegacyExternalStorage="true" in your manifest, this is to ensure that the library still works on Android 10. On Android 11>, there is no need to do anything.

I will update the library when Android 11 beta is released, it is still too early to make the changes now because a lot of things can still change in the Android R Preview (It's currently only at revision 2).

kneza23 commented 3 years ago

@HBiSoft but google states that apps with android:requestLegacyExternalStorage wont be able to be released anymore?

HBiSoft commented 3 years ago

@kneza23 wont be able to be released anymore where does Google say this?