skip to Main Content

Problem Statement

I am trying to make VCardfile, and It is successfully created but, I Want to share created file

Exception

android.os.FileUriExposedException: file:///storage/emulated/0/vcf_demonuts/android_1633671182515.vcf exposed beyond app through Intent.getData()

MY Code

            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:" + "[email protected]" + "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();

*I have to Try following&solution available on StackOverflow

I have created a provider_path.xml file

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths> 

Manifest

<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> 

2

Answers


  1. In the onCreate method your application file. Place the following code

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

    This will make the exception go away. However, this is used mostly as a diagnostic tool and it not a proper solution.

    Another solution with the fileProvider method would be to add FLAG_GRANT_READ_URI_PERMISSION flag to your intent.

    Intent intent = new Intent(Intent.ACTION_VIEW, FileProvider.getUriForFile(this, AUTHORITY, f));
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(intent);
    
    Login or Signup to reply.
  2. Try below code it works for sharing 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);
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search