有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java是一种共享的方式*。json文件是通过Intent还是其他方式?

在我的应用程序中,我需要分享*。json文件到其他应用程序(messenger、google disk等)。我如何通过意图或其他方式做到这一点

但当我试图通过意图实现这一点时,我遇到了一些问题

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“不支持的附件”


共 (2) 个答案

  1. # 1 楼答案

    我也有类似的问题,我发现这个article建议我们使用^{}

    它的作用是:

    FileProvider is a special subclass of ContentProvider that facilitates secure sharing of files associated with an app by creating a content:// Uri for a file instead of a file:/// Uri.

    我建议你看看这篇文章,如果你想要代码,也可以看看这个Stackoverflow post

  2. # 2 楼答案

    我认为您可以将该文件用作ExtraStream。下面的代码是共享图像文件,您可以将其更改为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"));