【发布时间】:2021-11-28 03:11:14
【问题描述】:
问题陈述
我正在尝试制作 VCardfile,它已成功创建,但我想分享创建的文件
例外
android.os.FileUriExposedException: file:///storage/emulated/0/vcf_demonuts/android_1633671182515.vcf 通过 Intent.getData() 暴露在应用之外
我的代码
var vdfdirectory = File(
Environment.getExternalStorageDirectory().toString() + VCF_DIRECTORY
);
// have the object build the directory structure, if needed.
if (!vdfdirectory.exists()) {
vdfdirectory.mkdirs();
}
vcfFile =
File(vdfdirectory, "android_" + Calendar.getInstance().getTimeInMillis() + ".vcf");
val fw: FileWriter = FileWriter(vcfFile);
fw.write("BEGIN:VCARD\r\n");
fw.write("VERSION:3.0\r\n");
// fw.write("N:" + p.getSurname() + ";" + p.getFirstName() + "\r\n");
fw.write("FN:" + "Sibtain" + "\r\n");
// fw.write("ORG:" + p.getCompanyName() + "\r\n");
// fw.write("TITLE:" + p.getTitle() + "\r\n");
fw.write("TEL;TYPE=WORK,VOICE:" + "+923155022905" + "\r\n");
// fw.write("TEL;TYPE=HOME,VOICE:" + p.getHomePhone() + "\r\n");
// fw.write("ADR;TYPE=WORK:;;" + p.getStreet() + ";" + p.getCity() + ";" + p.getState() + ";" + p.getPostcode() + ";" + p.getCountry() + "\r\n");
fw.write("EMAIL;TYPE=PREF,INTERNET:" + "abc@gmail.com" + "\r\n");
fw.write("END:VCARD\r\n");
fw.close();
val txtIntent = Intent(Intent.ACTION_SEND)
txtIntent.setDataAndType(Uri.fromFile(vcfFile), "text/x-vcard");
startActivity(Intent.createChooser(txtIntent, "Share"))
Toast.makeText(this, "Created!", Toast.LENGTH_SHORT).show();
*我已经尝试关注&solution available on stackoverflow
我已经创建了 provider_path.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
清单
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.testapp.provider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"
tools:replace="android:resource" />
</provider>
【问题讨论】:
标签: java android android-studio kotlin vcf-vcard