androidxmlkotlin

How do I share an image from my Android app (Kotlin)to other apps?


I am trying to allow users to share images from my App to other apps (such as Whatsapp, Telegram, gmail etc).

I am using Android Studio and my App is written in Kotlin.

Here is my MainActivity.kt:

package com.example.example

import android.app.AlertDialog
import android.app.DownloadManager
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Canvas
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.view.ContextMenu
import android.view.MenuItem
import android.view.View
import android.webkit.*
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.core.content.FileProvider
import java.io.File
import java.io.FileOutputStream
import java.net.URL

// OneSignal ID
const val ONESIGNAL_APP_ID = "REAL_KEY_HERE"

class MainActivity : ComponentActivity() {

    private lateinit var webView: WebView

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // OneSignal Initialization
    // (Your existing OneSignal code here)

    setContentView(R.layout.activity_main)
    webView = findViewById(R.id.webView)

    // Configure WebView settings
    configureWebViewSettings()

    // Set up WebViewClient
    setWebViewClient()

    // Set up WebChromeClient for handling file uploads
    setWebChromeClient()

    // Load the initial URL
    webView.loadUrl("https://www.example.com/Directory/AppHome.php")
}

private fun configureWebViewSettings() {
    val webSettings: WebSettings = webView.settings

    // Enable JavaScript in the WebView
    webSettings.javaScriptEnabled = true

    // Enable caching
    webSettings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK

    // Enable hardware acceleration
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        webView.setLayerType(View.LAYER_TYPE_HARDWARE, null)
    } else {
        webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
    }

    // Enable asynchronous loading
    webSettings.blockNetworkImage = false

    // Disable file access in the WebView
    webSettings.allowFileAccess = false

    // Disable insecure content (HTTP)
    webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_NEVER_ALLOW

    // Enable safe browsing only on devices with SDK version 27 or higher
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
        webSettings.safeBrowsingEnabled = true
    }

    webSettings.setDisplayZoomControls(false)

    // Disable metrics collection
    webSettings.allowContentAccess = false
    webSettings.allowFileAccess = false
    webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
}

private fun setWebViewClient() {
    webView.webViewClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
            // Load the URL within the WebView
            webView.loadUrl(request?.url.toString())
            return true
        }
    }
}

private fun setWebChromeClient() {
    registerForContextMenu(webView)
    webView.webChromeClient = object : WebChromeClient() {
        override fun onShowFileChooser(
            webView: WebView?,
            filePathCallback: ValueCallback<Array<Uri>>?,
            fileChooserParams: FileChooserParams?
        ): Boolean {
            // Handle file uploads if needed
            return true
        }
    }
}

override fun onCreateContextMenu(menu: ContextMenu?, v: View?, menuInfo: ContextMenu.ContextMenuInfo?) {
    super.onCreateContextMenu(menu, v, menuInfo)
    val hitTestResult = (v as WebView).hitTestResult
    if (hitTestResult.type == WebView.HitTestResult.IMAGE_TYPE ||
        hitTestResult.type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE
    ) {
        // If the user long-presses an image, show the context menu
        menu?.add(0, 1, 0, "Save or share notification")
    }
}

fun takeScreenshot(view: View): String? {
    // Create a bitmap of the view
    val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
    val canvas = Canvas(bitmap)
    view.draw(canvas)

    // Save the bitmap to a file
    val screenshotFile = File(Environment.getExternalStorageDirectory(), "screenshot.png")
    try {
        val fos = FileOutputStream(screenshotFile)
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos)
        fos.flush()
        fos.close()
        return screenshotFile.absolutePath
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return null
}

override fun onContextItemSelected(item: MenuItem): Boolean {
    if (item.itemId == 1) {
        val result = webView.hitTestResult
        if (result.type == WebView.HitTestResult.IMAGE_TYPE ||
            result.type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE
        ) {
            val imageUrl = result.extra
            if (!imageUrl.isNullOrEmpty()) {
                // Show a dialog with options: Download or Share External
                showImageOptionsDialog(imageUrl)
            } else {
                Toast.makeText(this, "No valid image URL found", Toast.LENGTH_SHORT).show()
            }
        }
        return true
    }
    return super.onContextItemSelected(item)
}

private fun showImageOptionsDialog(imageUrl: String) {
    val options = arrayOf("Download", "Share External")

    val builder = AlertDialog.Builder(this)
    builder.setTitle("Choose an action")
        .setItems(options) { dialog, which ->
            when (which) {
                0 -> downloadImage(imageUrl)
                1 -> shareImageExternal(imageUrl)
            }
            dialog.dismiss()
        }

    val dialog = builder.create()
    dialog.show()
}

private fun shareImageExternal(imageUrl: String) {
    // Download and save the image to a file
    val imageFilePath = downloadImageLocally(imageUrl)

    // Check if the file was downloaded successfully
    if (!imageFilePath.isNullOrBlank()) {
        // Create a content URI from the file path
        val imageFile = File(imageFilePath)
        val imageUri = FileProvider.getUriForFile(
            this,
            applicationContext.packageName + ".provider",
            imageFile
        )

        // Create an Intent to share the image
        val shareIntent = Intent(Intent.ACTION_SEND)
        shareIntent.type = "image/*"
        shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri)
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

        // Start the sharing activity
        startActivity(Intent.createChooser(shareIntent, "Share Image External"))
    } else {
        Toast.makeText(this, "Failed to download and share the image", Toast.LENGTH_SHORT).show()
    }
}

private fun downloadImage(imageUrl: String) {
    val request = DownloadManager.Request(Uri.parse(imageUrl))
        .setTitle("Image Download")
        .setDescription("Downloading")
        .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
        .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
        .setDestinationInExternalPublicDir(
            Environment.DIRECTORY_DOWNLOADS,
            "image.${getFileExtension(imageUrl)}"
        )

    val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
    downloadManager.enqueue(request)

    Toast.makeText(this, "Downloading IdentyAlert Notification", Toast.LENGTH_SHORT).show()
}

private fun downloadImageLocally(imageUrl: String): String? {
    try {
        val connection = URL(imageUrl).openConnection()
        connection.connect()
        val input = connection.getInputStream()

        val imageFile = File.createTempFile("shared_image", ".png", cacheDir)
        val output = FileOutputStream(imageFile)
        val buffer = ByteArray(1024)
        var bytesRead: Int

        while (input.read(buffer).also { bytesRead = it } != -1) {
            output.write(buffer, 0, bytesRead)
        }

        output.close()
        input.close()

        return imageFile.absolutePath
    } catch (e: Exception) {
        e.printStackTrace()
        return null
    }
}

private fun getFileExtension(url: String): String {
    val mimeTypeMap = MimeTypeMap.getSingleton()
    val extension = MimeTypeMap.getFileExtensionFromUrl(url)
    return mimeTypeMap.getMimeTypeFromExtension(extension)?.split("/")?.get(1) ?: "jpg"
}

}

This is my `provider_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
  <cache-path
    name="shared_images"
    path="/"/>
</paths>

This is my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />



<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@drawable/example_icon"
    android:label="@string/app_name"
    android:roundIcon="@drawable/identy_alert_icon"
    android:supportsRtl="true"
    android:theme="@style/Theme.example"
    tools:targetApi="31">

    <activity
        android:name=".MainActivity"
        android:exported="true"
        android:theme="@style/Theme.example">

        <meta-data android:name="android.webkit.WebView.EnableSafeBrowsing" android:value="true" />
        <meta-data android:name="android.webkit.WebView.MetricsOptOut" android:value="true" />

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
    </provider>

</application>

When the user press and hold the image, the user have two choices:

  1. Download
  2. Share External

The download options works perfect. The share option gives and error: "Failed to download and share the image"

I have tried various other methods,without success.

Can you please help me to Share the image to other apps?


Solution

  • In a quick code review, I see two problems.

    First, you use image/* for the MIME type. It is your content. You need to tell the other apps the actual MIME type, and you cannot reliably use a wildcard. In your case, your code is set up to save a .png file, so I am hoping your content is PNG, in which case the MIME type is image/png.

    Second, it appears that downloadImageLocally() is doing network I/O on the main application thread. You should see a stack trace in Logcat with NetworkOnMainThreadException. You need to move that work to a background thread.