Ruslan Leshchenko
Ruslan Leshchenko

Reputation: 4253

Universal way to share images to other apps

I'm developing an app which can send an image to other apps. After Nougat was released we should use FileProvider. But I noticed that this approach is not universal. For earlier versions of Android we should use Uri.getUriFromFile() method. But using FileProvider also doesn't work on Nougat for some apps (i.e. default messages app). Is there any universal method which will work with all apps, or I should define apps which can't work with FileProvider only on practice?

Upvotes: 1

Views: 40

Answers (1)

Andrey Zhukov
Andrey Zhukov

Reputation: 76

Following code must work properly:

    File file = new File(filePath);

    Uri contentUri = FileProvider.getUriForFile(getContext(), "com.example.fileprovider", file);

    if (contentUri != null) {

        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
        shareIntent.setDataAndType(contentUri, getContentResolver().getType(contentUri));
        shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);


        startActivity(Intent.createChooser(shareIntent, "Share image");

    }

Upvotes: 0

Related Questions