I'm trying to open a vcf file I've created by receiving data from a QrCode.
My code for open the file:
private fun openBackup(savedVCard: File) {
try {
val vcfMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("vcf")
val openVcfIntent = Intent(Intent.ACTION_VIEW)
openVcfIntent.setDataAndType(Uri.fromFile(savedVCard), vcfMimeType)
// Try to explicitly specify activity in charge of opening the vCard so that the user doesn't have to choose
// https://stackoverflow.com/questions/6827407/how-to-customize-share-intent-in-android/9229654#9229654
try {
if (mContext!!.packageManager != null) {
val resolveInfos = mContext!!.packageManager.queryIntentActivities(openVcfIntent, 0)
if (resolveInfos != null) {
for (resolveInfo in resolveInfos) {
val activityInfo = resolveInfo.activityInfo
if (activityInfo != null) {
val packageName = activityInfo.packageName
val name = activityInfo.name
// Find the needed Activity based on Android source files: http://grepcode.com/search?query=ImportVCardActivity&start=0&entity=type&n=
if (packageName != null && packageName == "com.android.contacts" && name != null && name.contains("ImportVCardActivity")) {
openVcfIntent.`package` = packageName
break
}
}
}
}
}
} catch (ignored: Exception) {
}
startActivity(openVcfIntent)
} catch (exception: Exception) {
Log.d("DEBUG", exception.toString())
}
}
And I receive this exception :
android.os.FileUriExposedException:
file:///storage/emulated/0/Android/data/pathformyapplication/files/qr.vcf exposed beyond app through Intent.getData()
I think the problem is the Uri.fromFile, I tried with Uri.parser with my vCard in String format but it's didn't work to.