首页 > 解决方案 > 有没有办法通过 Intent 或其他方式共享 *.json 文件?

问题描述

在我的应用程序中,我需要将 *.json 文件共享给其他应用程序(信使、谷歌磁盘等)。我怎样才能通过 Intent 或其他方式做到这一点?

但是当我尝试通过 Intent 执行此操作时,我遇到了一些问题。

override fun shareBackupData(path: String) {
        val uri = Uri.parse(path)
        val shareIntent = Intent()
        shareIntent.action = Intent.ACTION_SEND
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
        shareIntent.type = "*/*"
        startActivity(Intent.createChooser(shareIntent, "Choose"))
    }

当我运行此代码时,我选择要共享的应用程序,然后我看到 toast “不支持的附件”

标签: javaandroidandroid-intentkotlin

解决方案


我认为您可以将该文件用作 ExtrtaStream 以下代码共享图像文件您可以将其更改为您的 json 文件

final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpg");
final File photoFile = new File(getFilesDir(), "foo.jpg");//change it with your file
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
startActivity(Intent.createChooser(shareIntent, "Share image using"));

推荐阅读