fix(android): copy file to cache before querying + handle text/plain files#204
Closed
mertselimb wants to merge 2 commits intoachorein:mainfrom
Closed
fix(android): copy file to cache before querying + handle text/plain files#204mertselimb wants to merge 2 commits intoachorein:mainfrom
mertselimb wants to merge 2 commits intoachorein:mainfrom
Conversation
Fixes SecurityException when accessing content URIs from providers like Google Photos (MediaContentProvider) and Downloads (DownloadStorageProvider) that are not exported. The previous code called resolver.query() directly on the content URI, which fails when the temporary URI permission has expired or is inaccessible from the async context. Now copies the file to app cache via openInputStream first (while the permission is still valid), then reads metadata from the local copy. Fixes achorein#175
When sharing a .txt file (or any text/* file), Android sets the MIME type to text/plain but delivers the file via EXTRA_STREAM (not EXTRA_TEXT). The previous code checked the MIME type first and routed text/plain to the text handler, which read EXTRA_TEXT (null for file shares) — silently dropping the file. Now checks for EXTRA_STREAM first in ACTION_SEND. If a stream URI exists, it is always treated as a file regardless of MIME type. Text handling is only used when there is no stream attachment.
Owner
|
Thanks for the PR I will check that tomorrow. |
Author
|
I am using 54 but I am getting some flaky behavior on dev client. May need more tests. |
Author
|
Need more testing I had to close it. |
Author
|
@achorein I fixed my code with the code below it works now. I will try to create another pull when I have time. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes two Android bugs in
handleShareIntent/getFileInfo:Bug 1: SecurityException on shared files (fixes #175)
getFileInfocallsresolver.query(uri)directly on the content URI. On Android 12+, content providers like Google Photos (MediaContentProvider) and Downloads (DownloadStorageProvider) are not exported. The temporary URI permission granted byFLAG_GRANT_READ_URI_PERMISSIONcan expire before the asyncgetShareIntentcall reaches the native module, causing:Fix: Copy the file to app cache via
openInputStreamimmediately (while URI permission is valid), then read metadata from the local copy. Metadata query and MIME type resolution are wrapped in try-catch so a single provider failure doesn't crash the entire share flow.Bug 2: Files with
text/*MIME types silently droppedhandleShareIntentchecksintent.type.startsWith("text/plain")before checking forEXTRA_STREAM. When a file with atext/*MIME type (.txt,.csv,.html, etc.) is shared from a file manager, the MIME type istext/plain(or anothertext/*variant) but the file is delivered viaEXTRA_STREAM(notEXTRA_TEXT). The code enters the text branch, readsEXTRA_TEXT(null), and the share is silently lost.Fix: Check for
EXTRA_STREAMfirst inACTION_SEND. If a stream URI exists, always treat it as a file regardless of MIME type. Text handling is only used when there is no stream attachment.Testing
Tested with: