How to share video using file provider in android studio?

124 Views Asked by At

I am trying to share video from uri using file provider, but I can't understand how can I pass the uri to share video.

can anyone help me out of this problem

      <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.xyz.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_provider_paths" />
    </provider>

path.xml

<cache-path name="shared_videos" path="videos/"/>

here i am passing the uri

final String path = fileslist.get(position).getPath();
                String destPath = Environment.getExternalStorageDirectory().getAbsolutePath()+Constant.SAVE_FOLDER_NAME;
                Intent intent = new Intent(context,Video.class);
                intent.putExtra("Dest_path_video", destPath);
                intent.putExtra("File_video", path);
                intent.putExtra("FileName_video", modalClass.getFileName());
                intent.putExtra("Uri_video", modalClass.getUri().toString());
                context.startActivity(intent);

here is uri

String video_uri = fileslist.get(position).getUri().toString();

here how I am trying to share video

       Uri uri = null;
    try {
        File videoPath = new File(context.getCacheDir(), "videos");
        imagePath.mkdirs();
        File file = new File(videoPath , "shared_videos.mp4");
        uri = getUriForFile(context, "com.xyz.fileprovider", file);
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        intent.setType("video/*");
        context.startActivity(Intent.createChooser(intent, "Share Via"));
    } catch (Exception e) {
        Log.d("test",""+e.getMessage());
    }
1

There are 1 best solutions below

1
ninhnau19 On

make sure you have

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:grantUriPermissions="true"
            android:exported="false">
            ....
        </provider>

in AndroidManifest.xml.

I don't sure about getUriForFile method in your code, it should look like this:

FileProvider.getUriForFile(
                    context,
                    BuildConfig.APPLICATION_ID + ".provider",
                    file
                )

And can u provide an example URI of your video???