Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileNotFoundException in Android 11 #44

Open
mochadwi opened this issue Apr 20, 2021 · 11 comments
Open

FileNotFoundException in Android 11 #44

mochadwi opened this issue Apr 20, 2021 · 11 comments
Labels

Comments

@mochadwi
Copy link

https://developer.android.com/about/versions/11/behavior-changes-11

@mochadwi mochadwi added the bug label Apr 20, 2021
@mochadwi mochadwi changed the title FileNotFoundException when finding from cursor in Android 11 FileNotFoundException in Android 11 Apr 20, 2021
@mochadwi
Copy link
Author

mochadwi commented Apr 20, 2021

removing cursor line, fixes our issue:

class TusAndroidUpload(uri: Uri, context: Context) : TusUpload() {
    init {
        val resolver = context.contentResolver


        val fd = resolver.openFileDescriptor(uri, "r") ?: throw FileNotFoundException()
        val size = fd.statSize
        try {
            fd.close()
        } catch (e: IOException) {
            Log.e("TusAndroidUpload", "unable to close ParcelFileDescriptor", e)
        }
        setSize(size)
        inputStream = resolver.openInputStream(uri)
        fingerprint = String.format("%s-%d", uri.toString(), size)
        val metadata: MutableMap<String, String> = HashMap()
        metadata["filename"] = uri.path!!
        setMetadata(metadata)
    }
}

@mochadwi
Copy link
Author

current cursor in Android 11 seems doesn't work properly and returning null instead :(

@Acconut
Copy link
Member

Acconut commented May 4, 2021

Interesting report, thank you. Did you try enabling requestLegacyExternalStorage to get the behavior from Android 10 back (https://developer.android.com/about/versions/11/privacy/storage#scoped-storage)? This might be a temporary workaround until we find a solution to obtain the file name without a cursor.

Do you also have an alternative idea on how to obtain the file name?

@mochadwi
Copy link
Author

mochadwi commented May 5, 2021

Yes the requestLegacyExternalStorage is already added, but still fails >_<

We were still not aware of how the proper workaround using cursor currently 🙏 and still use the above approach instead.

@mochadwi
Copy link
Author

mochadwi commented May 5, 2021

For the filename, we're using our own Kotlin extension to get the filename from Uri. Lemme paste it here later (still afk)

@mochadwi
Copy link
Author

mochadwi commented May 5, 2021

fun Uri.fileName() = "$path".substring("$path".lastIndexOf("/") + 1)

This is a hackish ways though, we assume the last index as a file name from the suffix "/".

Any better suggestions?

In Java I think it's a little bit verbose than Kotlin version

@Acconut
Copy link
Member

Acconut commented May 5, 2021

Yes the requestLegacyExternalStorage is already added, but still fails >_<

Too bad, thanks for clarifying!

For the filename, we're using our own Kotlin extension to get the filename from Uri

I can see that this works for simple file selections, but might break when wanting to upload a file shared from another app. In this cases, the URI might be content://media/external/audio/media/710 where the last path segment does not correspond to the filename. We used the cursor to lookup the filename for this external content but apparently this is no forbidden since we are trying to access files from another app. See https://stackoverflow.com/a/27926504

I do not spend much time developing for Android these days, so I am not sure if there exists another approach for getting the filename. If not, it would be the best solution to just leave the filename empty if we get an FileNotFoundException. This way, the error is handled as gracefully as we can. Would you be interested in a PR for this?

@mochadwi
Copy link
Author

Will check the alternative ways to get the filename via cursor gracefully for Android 11 above, and then hopefully will submit the PR around end of month or early next month 🙏

@Acconut
Copy link
Member

Acconut commented May 11, 2021

Thank you! Looking forward to your contribution :)

@mochadwi
Copy link
Author

https://twitter.com/yrezgui/status/1405194690811596808?s=19

There's a modernstorage library by Google, something to look for after the release.

Because it's a tus-android is it okay to coupled with the android-related library? 🙏

@Acconut
Copy link
Member

Acconut commented Aug 10, 2021

There's a modernstorage library by Google, something to look for after the release.

Definitely! The modernstorage library definitely looks promising! Thanks for sharing

Because it's a tus-android is it okay to coupled with the android-related library?

Yes, exactly. tus-android-client is meant to contain Android-specific code :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants