android.os.FileUriExposedException: 通过 Intent.getData() Exception 暴露在 app 之外,同时共享 VCFile
android.os.FileUriExposedException: exposed beyond app through Intent.getData() Exception , While sharing VCFile
问题陈述
I am trying to make VCardfile, and It is successfully created but, I Want to share created file
异常
android.os.FileUriExposedException: file:///storage/emulated/0/vcf_demonuts/android_1633671182515.vcf exposed beyond app through 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 Whosebug
我已经创建了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 方法的解决方案是将 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);
}
}
问题陈述
I am trying to make VCardfile, and It is successfully created but, I Want to share created file
异常
android.os.FileUriExposedException: file:///storage/emulated/0/vcf_demonuts/android_1633671182515.vcf exposed beyond app through 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 Whosebug
我已经创建了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 方法的解决方案是将 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);
}
}