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

[android]: Unable to share document from some apps #109

Open
Mc231 opened this issue Dec 16, 2024 · 0 comments
Open

[android]: Unable to share document from some apps #109

Mc231 opened this issue Dec 16, 2024 · 0 comments

Comments

@Mc231
Copy link

Mc231 commented Dec 16, 2024

https://github.com/ShoutSocial/share_handler/blob/b0f0a7a243786d526ed7f8beee11fdf61ed2e9da/share_handler_android/android/src/main/kotlin/com/shoutsocial/share_handler/FileDirectory.kt#L34C1-L42C18

While sharing a document to various apps (e.g., Telegram, WhatsApp, etc.), everything worked as expected. However, when using my app, which integrates the share_handler SDK, the file path received was invalid and inaccessible. An example of the problematic file path was:
/storage/emulated/0/Android/data/my_org/files/Documents/AppFromWhereIShare/All/mic_20241213-180404.amr

I was managed to fix it by applying following changes to FileDirectory


...
    if (isExternalStorageDocument(uri)) {
        return getCachedDocumentPath(context, uri)
    }
...

/**
 * Copies the file referenced by the URI to the cache directory and returns its path.
 *
 * @param context The context.
 * @param uri The Uri to copy.
 */
private fun getCachedDocumentPath(context: Context, uri: Uri): String? {
    val fileName = uri.lastPathSegment?.split("/")?.lastOrNull() ?: "temp_${System.currentTimeMillis()}"
    val tempFile = File(context.cacheDir, fileName)

    return try {
        context.contentResolver.openInputStream(uri)?.use { input ->
            FileOutputStream(tempFile).use { output ->
                input.copyTo(output)
            }
        }
        Log.i("FileDirectory", "File copied to temp: ${tempFile.absolutePath}")
        tempFile.absolutePath
    } catch (e: Exception) {
        Log.e("FileDirectory", "Failed to copy file to temp: ${e.localizedMessage}")
        null
    }
}

Please consider applying this change or suggest an alternative solution to resolve the issue.

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

No branches or pull requests

1 participant