【问题标题】:Android - Cannot share vCard from internal cacheAndroid - 无法从内部缓存共享 vCard
【发布时间】:2026-02-02 21:55:01
【问题描述】:

我正在使用 ShareActionProvider 来共享我创建的 vcf 文件。

如果我将文件存储在外部缓存中,共享文件绝对没有问题,但如果我将其存储在内部缓存中,我尝试与之共享 vCard 的每个应用程序都会显示文件已损坏或不受支持。

我在创建文件后读取文件,在这两种情况下它们完全相同。

此代码有效:

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

但是,如果我改用 getCacheDir(),我就会遇到问题。

创建文件的代码如下:

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();
    }

这里是 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);
    }

对我做错了什么有任何想法吗?

【问题讨论】:

    标签: android caching file-sharing


    【解决方案1】:

    对我做错了什么有任何想法吗?

    我尝试与之共享 vCard 的每个应用都说文件已损坏或不受支持。

    根据Using the Internal Storage

    您可以将文件直接保存在设备的内部存储中。默认情况下,保存到内部存储的文件是您的应用程序私有的其他应用程序无法访问它们(用户也不能)...

    因此,建议使用外部存储

    清单

    使用权限 android:name="android.permission.WRITE_EXTERNAL_STORAGE"

    代码

    ```

    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);
    }
    

    ```

    【讨论】: