-
-
Notifications
You must be signed in to change notification settings - Fork 88
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
[super_clipboard] App crashes on Android after copying an image and restart the app #435
[super_clipboard] App crashes on Android after copying an image and restart the app #435
Comments
A quick workaround
Same solution in Kotlin/JVM can be used in Java. // Check if you have access to the URI and the file exists before processing the URI:
/**
* A method to see if any exceptions can occur
* before start reading the file.
*
* The app can lose access to the [Uri] due to lifecycle changes.
*
* @throws SecurityException When the app loses access to the [Uri] due to app lifecycle changes
* or app restart.
* @throws FileNotFoundException Could be thrown when the [Uri] is no longer on the clipboard.
* */
@Throws(Exception::class)
private fun Uri.readOrThrow(
context: Context,
) = try {
context.contentResolver.openInputStream(this)?.close()
} catch (e: Exception) {
throw e
}
try {
imageUri.readOrThrow(context)
} catch (e: Exception) {
when (e) {
is SecurityException -> result.error(
"FILE_READ_PERMISSION_DENIED",
"An image exists on the clipboard, but the app no longer " +
"has permission to access it. This may be due to the app's " +
"lifecycle or a recent app restart: ${e.message}",
e.toString(),
)
is FileNotFoundException -> result.error(
"FILE_NOT_FOUND",
"The image file can't be found, it might not be on the system clipboard anymore: ${e.message}",
e.toString()
)
else -> result.error(
"UNKNOWN_ERROR_READING_FILE",
"An unknown occurred while reading the image file URI: ${e.message}",
e.toString()
)
}
return
}
// Process further with the URI... Which doesn't fix the issue completely but at least no longer app crashes, since if you try to open the app and you still have that image on your clipboard without copying something else, the app will continue to crash and the system will recommend the user to uninstall the app or report it to the developer, the user will probably delete the app before reaching this state. So it's probably better to return Saving an image to a temporary location is not guaranteed as it can be erased, somewhere in the app document directory is not ideal if we have to save all images on the clipboard to retain access to them on app restart, the plugin might not have full access to the app lifecycle to delete them later or the user don't want to save such images on the user device. |
I'm still uncertain why sometimes I get What I'm sure about this issue doesn't happen when not pasting that image into the app (step 2 removed) |
This is an issue when using super_clipboard however the native platform code is in
super_native_extensions
.Steps to reproduce:
flutter run
), you might get a crash.Crash Log
Video
Android Emulator API 34.
SuperClipboardAndroidCrash.mov
Similar but not related issue
This issue is not related and doesn't use
super_clipbaord
however a similar issue will occur with the same steps to reproduce.It uses our custom implementation in Flutter Quill #2230 using Kotlin (for Android) directly using the Flutter method channel without Rust.
Screen.Recording.2024-09-18.at.4.53.08.PM.mov
The difference is that the app doesn't crash:
It's handled in a way so it returns null and throws an exception instead though I'm not entirely sure about the exact issue with
super_clipbaord
implementation.You might noticed that both
quill_native_bridge
(QuillNativeBridgePlugin
) andsuper_clipbaord
havejava.lang.SecurityException: Permission Denial
in the console, which confirms that it's a security issue related to the lifecycle of the app.Sometimes it can be:
Or:
The only option I had was to write this image to a temporary location or internal storage, then once paste it again on app restart (when not having access to it), then get access to the already saved image, which seems like a workaround to me, there are similar issues on Android that need to solved manually and can be error prone such as process death. So I tried
super_clipboard
to see how it's handled, and the impl we have doesn't have the exact same exception details assuper_clipboard
, it seems like a similar unrelated issue.Hint: The image URI that is given by the
android.content.ClipboardManager
gives the app limited access, and can be restricted to its lifecycle, once the lifecycle is destroyed, the app no longer has access to theandroid.net.Uri
.The
super_clipboard
plugin could check if it has access before processing further, avoid resulting in a crash, and returnnull
(false
for thecanProvide
or throw a dart platform exception) or save the image to somewhere and get access to the image later on app restart and return the image.let me know If more details are needed or unable to reproduce the issue.
The text was updated successfully, but these errors were encountered: