打开vcf文件异常

时间:2018-03-21 20:32:51

标签: android file exception kotlin vcf

我试图打开一个我通过从QrCode接收数据而创建的vcf文件。

我打开文件的代码:

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())
    }
}

我收到了这个例外:

android.os.FileUriExposedException:

 file:///storage/emulated/0/Android/data/pathformyapplication/files/qr.vcf exposed beyond app through Intent.getData()

我认为问题是Uri.fromFile,我尝试使用字符串格式的Uri.parser和我的vCard,但它没有用。

0 个答案:

没有答案
相关问题