【问题标题】:Two file providers in android app image fileprovider not workingandroid应用程序图像文件提供程序中的两个文件提供程序不起作用
【发布时间】:2021-11-05 07:40:16
【问题描述】:

我正在与文件提供商共享应用程序,并且我想共享位图中的图像。 我有两个文件提供程序,第一个用于共享 apk,第二个用于共享图像。 apk 文件提供程序运行良好,但图像共享文件提供程序不共享图像。如果我将图片分享到 WhatsApp,Gmail 会说文件格式不支持。

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>

如果我运行此共享意图正在打开。但分享到 WhatsApp、Gmail 或它所说的文件格式不受支持。帮帮我,我不知道如何解决这个问题。

【问题讨论】:

  • catch (e: IOException) { e.printStackTrace() } 有异常为什么不停下来?并通知用户!?
  • 为什么两次 FileProvider?最好用一个。

标签: android android-studio android-intent bitmap android-fileprovider


【解决方案1】:

我是通过直接分享位图图像来解决的。

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 2022-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-04
  • 1970-01-01
相关资源
最近更新 更多