1

In my app I need to share *.json file to other app (messenger, google disk, etc). How can I do this via Intent or something else?

But when I trying to do this via Intent, I have some problems.

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

When I run this code, I choose app to share and then I see toast "unsupported attachment"

2 Answers2

0

i think you can use the file as ExtrtaStream the following code is sharing image file you can change it to your json file

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"));
0

I had similar problems with this and I found this article where it recommends us to use FileProvider.

What it does is :

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.

I recommend you to take a look to the article and also if you want code, take a look at this Stackoverflow post

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148