Search code examples
androidandroid-studioandroid-intentbitmapandroid-fileprovider

Two file providers in android app image fileprovider not working


I am sharing the app with the file provider and I want to share the image from the bitmap. I have two file providers number one is for share apk, number two for share image. apk file provider works good but the image share file provider not sharing images. if I share an image to WhatsApp, Gmail it says file format does not support it.

fileprovider.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-cache-path
    name="apk"
    path="/"/>
</paths>

provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-cache-path name="img" path="my_images/"/>
</paths>

imagesharefrombitmap.kt

  imgBtnShare.setOnClickListener {
        shareImage()
        loadingDialoglordshiva.startDialog()
    }

}

private fun shareImage() {
    val bitmapShare = getBitmapFromView(relLayout)
    val filename = "${System.currentTimeMillis()}.jpg"
    val cachePath = File(externalCacheDir.toString() + "/my_images")
    cachePath.mkdirs()

    //create a png file

    //create png file
    val file = File(cachePath, filename)
    val fileOutputStream: FileOutputStream
    try {
        fileOutputStream = FileOutputStream(file)
        bitmapShare.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream)
        fileOutputStream.flush()
        fileOutputStream.close()
    } catch (e: FileNotFoundException) {
        e.printStackTrace()
    } catch (e: IOException) {
        e.printStackTrace()
    }
    val myImageFileUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider_paths",file)

    //create a intent

    //create a intent
    val intent = Intent(Intent.ACTION_SEND)
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    intent.putExtra(Intent.EXTRA_STREAM, myImageFileUri)
    intent.type = "image/jpg"
    startActivity(Intent.createChooser(intent, "Share with"))
    loadingDialoglordshiva.dismissDialog()
}

Manifest_file_only_provider_showed

  <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.lordshiva.myapplication.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/fileprovider"
            />
    </provider>

    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.lordshiva.myapplication.provider_paths"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
    </provider>

if i run this share intent is opening. but share to WhatsApp,Gmail or something its says file format is not supported . help me I don't know how to solve this.


Solution

  • I am solved by share bitmap image directly to intent.