Android - Cannot share vCard from internal cache

354 views Asked by At

I am using a ShareActionProvider to share a vcf file I created.

If I store the file in the external cache, I have absolutely no problems sharing the file, but if I store it in the internal cache, every app I try to share the vCard with says the file is corrupted or unsupported.

I read the file after creating it, and in both cases they are exactly the same.

This code works:

File dir = new File(getExternalCacheDir() + "/contact");
dir.mkdirs();
vcfFile = new File(dir, name.replace(' ', '+') + ".vcf");

However, if I use getCacheDir() instead, I get the problem.

Here's the code for creating the file:

FileWriter fw;
    try {
        fw = new FileWriter(vcfFile);
        fw.write("BEGIN:VCARD\r\n");
        fw.write("VERSION:2.1\r\n");
        fw.write("N:" + codedName + "\r\n");
        fw.write("FN:" + name + "\r\n");
        fw.write("ORG:" + org + "\r\n");
        fw.write("TITLE:" + position + "\r\n");
        fw.write("TEL;PREF;WORK;VOICE;ENCODING=QUOTED-PRINTABLE:" + phone + "\r\n");
        fw.write("TEL;PREF;WORK;FAX;ENCODING=QUOTED-PRINTABLE:" + fax + "\r\n");
        fw.write("ADR;WORK;;ENCODING=QUOTED-PRINTABLE:" + codedAddr + "\r\n");
        fw.write("EMAIL;INTERNET:" + email + "\r\n");
        fw.write("URL;WORK:" + website + "\r\n");
        fw.write("PHOTO;TYPE=JPEG;ENCODING=BASE64:" + codedImage + "\r\n");
        fw.write("END:VCARD\r\n");
        fw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

And here's the code for the ShareActionProvider:

provider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();

    if (provider != null) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("text/vcard");
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(vcfFile));
        provider.setShareIntent(intent);
    }

Any ideas of what I'm doing wrong?

1

There are 1 answers

0
kedoska On

Any ideas of what I'm doing wrong?

every app I try to share the vCard with says the file is corrupted or unsupported.

According with Using the Internal Storage

You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user)...

For this reason, it is advisable to use External Storage

Manifest

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

Code

```

public void sharePublicContact(View view){

    String name = "Mickey Mouse";
    String org = "Disney Corp.";
    String note = "";

    File dir = new File(getExternalCacheDir() + "/contact");
    dir.mkdirs();
    File vcfFile = new File(dir, name.replace(' ', '+') + ".vcf");

    FileWriter fw;
    try {

        fw = new FileWriter(vcfFile);
        fw.write("BEGIN:VCARD\r\n");
        fw.write("VERSION:3.0\r\n");
        fw.write("FN:" + name + "\r\n");
        fw.write("ORG:" + org + "\r\n");
        fw.write("NOTE:" + note + "\r\n");
        fw.write("END:VCARD\r\n");
        fw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.setType("text/vcard");
    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(vcfFile));

    startActivity(sendIntent);
}

```