如何在android工作室中使用文件提供商共享视频



我正在尝试使用文件提供程序共享来自uri的视频,但我不明白如何传递uri来共享视频。

有人能帮我解决这个问题吗

<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/"/>

我在这里通过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);

这是uri

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

以下是我如何尝试共享视频

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());
}

确保您有

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

AndroidManifest.xml.中

我不确定代码中的getUriForFile方法,它应该是这样的:

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

你能提供一个视频的URI示例吗???

最新更新