File from FileProvider can't be viewed

冷暖自知 提交于 2021-01-28 09:40:39

问题


I'm struggling with FileProvider. I want to open a video in another app, but no matter what I try every single app says that the video can't be loaded.

private void passVideo2(String videoname) {
    File videoPath = new File(Environment.getExternalStorageDirectory(), "video_folder");
    File videoInternalPath = new File(this.getFilesDir(), videoname);
    File newFile = new File(videoPath, videoname);
    Uri uri = FileProvider.getUriForFile(this, "com.example.provider", newFile);
    Intent viewIntent = new Intent(Intent.ACTION_VIEW, uri);

    viewIntent.setDataAndType(uri, getContentResolver().getType(uri));
    viewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    this.startActivity(viewIntent);
}

I've tried getting the video from the external and from the internal storage and I've tried different video formats and other file types like e.g. pdf and nothing seems to work

This is my provider_paths.xml

<paths>
    <files-path name="files" path="/"/>
    <external-path name="external" path="video_folder"/>
</paths>

and this is from the manifest

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

来源:https://stackoverflow.com/questions/61162568/file-from-fileprovider-cant-be-viewed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!