android.os.FileUriExposedException:在共享vc文件时,通过Intent.getData



问题说明

我正在尝试制作VCardfile,并且它已成功创建,但是,我想共享创建的文件

android.os。FileUriExposedException:文件:///存储/模拟/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:VCARDrn");
fw.write("VERSION:3.0rn");
// fw.write("N:" + p.getSurname() + ";" + p.getFirstName() + "rn");
fw.write("FN:" + "Sibtain" + "rn");
//  fw.write("ORG:" + p.getCompanyName() + "rn");
//  fw.write("TITLE:" + p.getTitle() + "rn");
fw.write("TEL;TYPE=WORK,VOICE:" + "+923155022905" + "rn");
//   fw.write("TEL;TYPE=HOME,VOICE:" + p.getHomePhone() + "rn");
//   fw.write("ADR;TYPE=WORK:;;" + p.getStreet() + ";" + p.getCity() + ";" + p.getState() + ";" + p.getPostcode() + ";" + p.getCountry() + "rn");
fw.write("EMAIL;TYPE=PREF,INTERNET:" + "abc@gmail.com" + "rn");
fw.write("END:VCARDrn");
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();

*我必须尝试以下&解决方案可在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> 

onCreate方法中您的应用程序文件。输入以下代码

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());

这将使异常消失。然而,这主要是用作诊断工具,它不是一个合适的解决方案。

fileProvider方法的另一个解决方案是在intent中添加FLAG_GRANT_READ_URI_PERMISSION标志。

Intent intent = new Intent(Intent.ACTION_VIEW, FileProvider.getUriForFile(this, AUTHORITY, f));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);

试试下面的代码,它可以用于共享VCFile:

private void shareContact(ContactsModal modal) {
String lookupKey = null;
Cursor cur = ((Activity)context).getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, new String[]
{ ContactsContract.Contacts.LOOKUP_KEY },
ContactsContract.Contacts._ID + " = " + modal.getContactID(), null, null);
if (cur.moveToFirst()) {
lookupKey = cur.getString(0);
}
if(lookupKey!=null){
Uri vcardUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(ContactsContract.Contacts.CONTENT_VCARD_TYPE);
intent.putExtra(Intent.EXTRA_STREAM, vcardUri);
intent.putExtra(Intent.EXTRA_SUBJECT, "Contact Name");
((Activity)context).startActivity(intent);
}
}

相关内容

  • 没有找到相关文章

最新更新